09-14-2009, 12:06 AM
#1 (permalink )
Registered Member
Join Date: Jun 2009
Location: Australia
Posts: 180
download many UIImage from NSConnection at once
Im trying to load 2 or more http images at once, but seems like there can only be 1 connection at 1 time,
whats the way of doing this, or continuously download UIImage one after another?
09-14-2009, 12:22 AM
#2 (permalink )
Registered Member
Join Date: May 2009
Location: Stanford, CA
Posts: 289
Well... you could use threading.
Not recommended unless you know about it but its an option personally I have never done threading but its a place to start
09-14-2009, 08:44 AM
#3 (permalink )
Humbled Student
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
This is how I do it. This is asynchronous so it will not tie up the main thread.
.h file
Code:
NSURLConnection *connection;
NSMutableData* data;
UIImage *image;
YourParentViewController *myParentController ;
-(void)startDownload:(NSString *)url withParentController:(YourParentViewController *)parent;
@property (nonatomic, retain) NSURLConnection *connection;
@property (nonatomic, retain) NSMutableData* data;
@property (nonatomic, retain)UIImage *image;
@property (nonatomic, retain) YourParentViewController *myParentController;
.m file
Code:
-(void)startDownload:(NSString *)url withParentController:(YourParentViewController *)parent{
[self setMyParentViewController:parent];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
if (data==nil) data = [[NSMutableData alloc] initWithCapacity:2048];
[data appendData:incrementalData];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
[self setImage: [UIImage initWithData:data]];
[myParentController processImage:[self image]];
data=nil;
}
you would probably want to put this code in its own object (called for example DownloadImage). then you can download the images like this:
Code:
-(void)downloadTwoImages{
//Make this a property somewhere in your H file.
if (! imagesArray) imagesArray=[[NSMutableArray alloc] init];
[imagesArray removeAllObjects];
DownloadImage *imageDownloader=[[[DownloadImage alloc] init] autorelease];
[imageDownloader startDownload:@"http://yourdomain/image1.png" withParentController:self];
[imageDownloader startDownload:@"http://yourdomain/image2.png" withParentController:self];
}
-(void) processImage:(UIImage *)image{
[imagesArray addObject:image];
}
Last edited by Dutch; 09-14-2009 at 08:48 AM .
09-16-2009, 07:45 PM
#4 (permalink )
Registered Member
Join Date: Jun 2009
Location: Australia
Posts: 180
how do i use this method and apply in UITableView's Image?
12-27-2009, 11:01 AM
#5 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 201
Quote:
Originally Posted by
Dutch
This is how I do it. This is asynchronous so it will not tie up the main thread.
.h file
Code:
NSURLConnection *connection;
NSMutableData* data;
UIImage *image;
YourParentViewController *myParentController ;
-(void)startDownload:(NSString *)url withParentController:(YourParentViewController *)parent;
@property (nonatomic, retain) NSURLConnection *connection;
@property (nonatomic, retain) NSMutableData* data;
@property (nonatomic, retain)UIImage *image;
@property (nonatomic, retain) YourParentViewController *myParentController;
.m file
Code:
-(void)startDownload:(NSString *)url withParentController:(YourParentViewController *)parent{
[self setMyParentViewController:parent];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
if (data==nil) data = [[NSMutableData alloc] initWithCapacity:2048];
[data appendData:incrementalData];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
[self setImage: [UIImage initWithData:data]];
[myParentController processImage:[self image]];
data=nil;
}
you would probably want to put this code in its own object (called for example DownloadImage). then you can download the images like this:
Code:
-(void)downloadTwoImages{
//Make this a property somewhere in your H file.
if (! imagesArray) imagesArray=[[NSMutableArray alloc] init];
[imagesArray removeAllObjects];
DownloadImage *imageDownloader=[[[DownloadImage alloc] init] autorelease];
[imageDownloader startDownload:@"http://yourdomain/image1.png" withParentController:self];
[imageDownloader startDownload:@"http://yourdomain/image2.png" withParentController:self];
}
-(void) processImage:(UIImage *)image{
[imagesArray addObject:image];
}
With the approach you are using for multiple images, are you able to control the order in which they are added to the array? From looking at this code, it looks like the first one to finish downloading will be added to the array, even if it was the second one called. I'm struggling to figure out how to load multiple images one after each other using NSURLConnection. I can download one image no problem using NSURLConnection, but the right way to fire off multiple downloads for 3-4 images isn't clear to me.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 251
25 members and 226 guests
ADY , AragornSG , bookesp , chillyh , ckgni , dacapo , Dani77 , Davey555 , Desert Diva , Dominus , glenn_sayers , HemiMG , JasonR , LEARN2MAKE , M.A.S. , marshusensei , mer10 , nobre84 , prchn4christ , Raggou , Rudy , ryantcb , themathminister , theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,765
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp