views:

2234

answers:

3

I implemented lazy image load for my UITableView using NSUrlConnection. This is all working very nicely. When I open my table, I automatically get the images when I wait for a second (on 3G). However, when I scroll, the table loads the new cell's, starts the NSURLConnections, but when the image is finished loading (in code), they do not get put into the view until the table actually stops scrolling..

The Youtube application is able to load the images into the table WHILE scrolling, I'd like to do this as well, any hints / pointers?

+4  A: 

Take a look at the Apple example LazyTableImages.

If you request all the images for your table asynchronously, they will load as they arrive.

You'll notice some applications wait for scrollViewDidEndDragging and loadImagesForOnscreenRows to be truly lazy and only request images for rows the user is currently examining.

Nuthatch
Actually that example works even worse then mine, because it only starts the thread to load the image when the table stops scrolling (not even when it comes into view). I'll take a look at how they do threading though.. My current app loads the rows as they come in, and stops them if you scroll past the image. The only problem is that they are not inserted into the table untill the table stops scrolling, and I don't exactly know why. I think even though my image loading occurs asynchronously, it's not really in a separate thread, hence it waits for the table to stop scrolling.
StijnSpijker
+2  A: 

I just found my answer thanks to the 'Related' feature to the right.. http://stackoverflow.com/questions/1826913/delayed-uiimageview-rendering-in-uitableview

You have to start NSUrlConnection in a different run-loop, so that you receive data while the table is scrolling.

Thanks for the answers! (My answer in code:)

    NSURLRequest* request = [NSURLRequest requestWithURL: imageUrl cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 30.0];
    connection = [[NSURLConnection alloc] initWithRequest: request delegate: self startImmediately: NO];
    [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    [connection start];
StijnSpijker
A: 

Hi StijnSpijker,

Your solution works perfectly well, I have one problem

while searching the table which has got more than 6 rows.

Once the page loads with default 6 rows all the 6 images for each row loads fine, then, I entered any search term in search box, the rows matching the term will be after 6th row (for which the images are not yet loaded), The lazyimage code loads images based on the (NSIndexPath *)indexPath. As the first default 6 rows images are already loaded, then the search results rows, for which, the images are not yet loaded, are unable to load their images, bcoz the indexPath 1 to 6 are already loaded with images.

Mohan