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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-14-2009, 12:06 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Australia
Posts: 180
Default 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?
svveet is offline   Reply With Quote
Old 09-14-2009, 12:22 AM   #2 (permalink)
Registered Member
 
msencenb's Avatar
 
Join Date: May 2009
Location: Stanford, CA
Posts: 289
Default

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
__________________
I'm starting a new blog dedicated to iOS development. Check it out at:

http://www.iosdevscreencasts.com
msencenb is offline   Reply With Quote
Old 09-14-2009, 08:44 AM   #3 (permalink)
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
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 08:48 AM.
Dutch is offline   Reply With Quote
Old 09-16-2009, 07:45 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Australia
Posts: 180
Default

how do i use this method and apply in UITableView's Image?
svveet is offline   Reply With Quote
Old 12-27-2009, 11:01 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 201
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
Reply

Bookmarks

Tags
nsconnection, uiimage

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:35 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0