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 > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-13-2011, 06:29 AM   #1 (permalink)
Beginner iOS Dev
 
Join Date: Feb 2011
Location: Boston
Posts: 372
cityofangels is on a distinguished road
Default How To Transfer Download of Images To Background Thread?

I have this method and I was advised to do the download of images on the background thread. Can anyone help me with this?

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;
                break;
            }
        }
    }

    NSURL *myURL=[NSURL URLWithString:[self.picturesArray objectAtIndex:indexPath.row]]; 
    NSData *myData1 = [[NSData alloc] initWithContentsOfURL:myURL];
    UIImage *myImage = [[UIImage alloc] initWithData:myData1]; 

    cell.imageView.image = myImage;

    return cell;
}
cityofangels is offline   Reply With Quote
Old 09-13-2011, 07:02 AM   #2 (permalink)
myCustomUserTitle
 
marketshow's Avatar
 
Join Date: Dec 2010
Location: Athens
Age: 27
Posts: 79
marketshow is on a distinguished road
Default

Quote:
Originally Posted by cityofangels View Post
I have this method and I was advised to do the download of images on the background thread. Can anyone help me with this?

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;
                break;
            }
        }
    }

    NSURL *myURL=[NSURL URLWithString:[self.picturesArray objectAtIndex:indexPath.row]]; 
    NSData *myData1 = [[NSData alloc] initWithContentsOfURL:myURL];
    UIImage *myImage = [[UIImage alloc] initWithData:myData1]; 

    cell.imageView.image = myImage;

    return cell;
}

One of the best examples for asynchronous image download and caching is this : http://ezekiel.vancouver.wsu.edu/~wa...llowJacket.zip.
marketshow is offline   Reply With Quote
Old 09-13-2011, 01:41 PM   #3 (permalink)
Beginner iOS Dev
 
Join Date: Feb 2011
Location: Boston
Posts: 372
cityofangels is on a distinguished road
Default

Quote:
Originally Posted by marketshow View Post
One of the best examples for asynchronous image download and caching is this : http://ezekiel.vancouver.wsu.edu/~wa...llowJacket.zip.
Thanks, but that example is very complicated. I simply want to load the downloading on the background thread so it doesn't slow down the UI.
cityofangels is offline   Reply With Quote
Old 09-13-2011, 02:13 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by cityofangels View Post
Thanks, but that example is very complicated. I simply want to load the downloading on the background thread so it doesn't slow down the UI.
Threads are resource hogs, and best avoided.

You are much better served to use NSURLConnection and NSURLRequest to create an async download on the same thread. It takes about a page of code to do. You create an NSURL for the remote file. You create an NSURLRequest that represents your request for a remote file. You create an NSURLConnection object to manage the connection to the remote host. You set yourself as the delegate of the NSURLConnection. You start the download running, and handle 2 or 3 delegate methods that notify you as data arrives, and when you get a message that all the data has been delivered, you save the data to disk.

Alternately, you can use the ASIHTTPRequest library to do this. I haven't used it before, but supposedly it makes doing async downloading a snap. (just a couple of lines of code.) Do a google on ASIHTTPRequest to find it.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-13-2011, 02:42 PM   #5 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

I spot a slow scrolling tableView...

Look into GCD - Cocoa Touch Tutorial: Using Grand Central Dispatch for Asynchronous Table View Cells | Jeff Kelley?s Blog

Quote:
Originally Posted by cityofangels View Post
I have this method and I was advised to do the download of images on the background thread. Can anyone help me with this?

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;
                break;
            }
        }
    }

    NSURL *myURL=[NSURL URLWithString:[self.picturesArray objectAtIndex:indexPath.row]]; 
    NSData *myData1 = [[NSData alloc] initWithContentsOfURL:myURL];
    UIImage *myImage = [[UIImage alloc] initWithData:myData1]; 

    cell.imageView.image = myImage;

    return cell;
}
iSDK is offline   Reply With Quote
Reply

Bookmarks

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: 404
10 members and 394 guests
7twenty7, Eclectic, eski, fiftysixty, iOS.Lover, JackReidy, teebee74, tim0504, UMAD, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

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