Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 12-27-2009, 10:01 AM   #5 (permalink)
not_too_shabby
Registered Member
 
Join Date: Feb 2009
Posts: 202
not_too_shabby is on a distinguished road
Default

Quote:
Originally Posted by Dutch View Post
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.
not_too_shabby is offline   Reply With Quote
 

» Advertisements
» Online Users: 675
19 members and 656 guests
ADY, apatsufas, dacapo, djbrooks111, Duncan C, esoteric, fiftysixty, iAppDeveloper, ipodphone, JoePitto, KarlJay, mutantskin, sacha1996, SLIC, smithdale87, smithms, spinyanteater, ukneeq, Xaphann
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,422
Threads: 94,017
Posts: 402,542
Top Poster: BrianSlick (7,978)
Welcome to our newest member, iAppDeveloper
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:04 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.