Skip to main content

Lazy load images in UITableView


I have some 50 custom cells in my UITableView. I want to display an





image and a label in the cells where I get the images from URLs.



I want to do a lazy load of images so the UI does not freeze up while

the images are being loaded. I tried getting the images in separate

threads but I have to load each image every time a cell becomes

visible again (Otherwise reuse of cells shows old images) Can someone please tell

me how to duplicate this behavior.



Any other suggestions are welcome



Thanks.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. checkout
    https://github.com/rs/SDWebImage

    README Section says,how to use it in your app.

    ReplyDelete
  2. There's a vey good official example here from Apple :
    http://developer.apple.com/iphone/library/samplecode/LazyTableImages/index.html
    I think you'll find the right approach in there....

    ReplyDelete
  3. I made a free library designed just for this... HJCache. Its very easy to use.
    http://www.markj.net/hjcache-iphone-image-cache/

    ReplyDelete
  4. Jeff LaMarche (author of Beginning iPhone Development) has written a tutorial on how to do this: http://iphonedevelopment.blogspot.com/2010/05/downloading-images-for-table-without.html

    His example doesn't use threads and does cached images that have already been loaded.

    ReplyDelete
  5. I'd suggest going for an NSOperation and doing whatever you need on another thread.

    This is a class I wrote for image loading:

    - (id)initWithTarget:(id)trgt selector:(SEL)sel withImgURL:(NSString *)url {
    if(self = [super init]) {
    if(url == nil || [url isEqualToString:@""])
    return nil;
    target = trgt;
    action = sel;
    imgURL = [[NSURL alloc] initWithString: url];
    }
    return self;
    }

    - (void)main {
    [NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil];
    }

    - (void)loadImage {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    UIImage *img = [UIImage imageNamed: @"default_user.png"];
    if(![[imgURL absoluteString] isEqualToString: @"0"]) {
    NSData *imgData = [NSData dataWithContentsOfURL: imgURL];
    img = [UIImage imageWithData: imgData];
    }
    if([target respondsToSelector: action])
    [target performSelectorOnMainThread: action withObject: img waitUntilDone: YES];
    [pool release];
    }

    - (void)dealloc {
    [imgURL release];
    [super dealloc];
    }


    Hope that helps!

    ReplyDelete
  6. You can also try EGOImageLoading. There is also a demo, which shows how to use it.

    ReplyDelete
  7. I think there is another way to solve that problem if you want to load that image then at the start of the view thats mean

    when -(void)loadView is loading, then just allocate these images and take it into a nsarray now when table cell is loading then make your view at the table cell and according to the indexPath.row just replace these images of nsarray into that view as a background image or make subview of these images on that new view of tablecell using nsarray index and indexPath.row of the tableview cell.

    ReplyDelete
  8. I am not aware of any built in way to accomplish this but it sounds like you have something working with another thread already.

    Assuming that your only remaining problem now is invalidating the contents of a cell when it comes back into view you should look at:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath


    By implementing this delegate method you could check the contents of the cell before it draws to see if it needs new contents. If it does then you could empty the cells contents and trigger your threaded loading again.

    You might also consider posting some code.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex