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 09-14-2009, 07:44 AM   #3 (permalink)
Dutch
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
Dutch will become famous soon enough
Send a message via AIM to Dutch
Default

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 07:48 AM.
Dutch is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 175,183
Threads: 93,923
Posts: 402,162
Top Poster: BrianSlick (7,968)
Welcome to our newest member, gab20
Powered by vBadvanced CMPS v3.1.0

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