Apple LazyTableImages Sample Question - Doesn’t Behave Just Like the App Store

I have a UITableView with a list of elements, each of which has its own image. I thought that the Apple LazyTableImages sample sample would be ideal to study and use to implement the same process of loading images asynchronously after receiving the initial list data.

For the most part, it works quite well, except that I noticed a slight difference in behavior between this example application and the way the actual application store loads images.

If you run the LazyTableImages sample, do a quick scroll down, you will see that the images are not displayed until the scrolling is complete.

Now, if you perform the same test with a list of items in the actual application store, you will see that the images begin to appear as soon as new items appear, even if the scrolling is not stopped.

I am trying to achieve the same results, but so far I am not moving forward. Does anyone have any ideas on how to do this?

Thanks!

+7
source share
2 answers

I am puzzled that no one can answer this question ...

So, in the end, I figured out how to achieve the exact effect that is used in the actual app store in terms of how the icons are loaded / displayed.

Take a sample LazyTableImages project and make some simplified modifications.

  • Go to the root view controller and remove all checks regarding scrolling and / or slowing down the table in cellForRowAtIndexPath

  • Remove all calls to loadImagesForOnScreenRows and thus remove this method.

  • Go to IconDownload.m and change the startDownload method to not make the async image downlaod, but instead load synchronization in the background thread. Delete all the code in startDownload and add the following so that it looks like this:

- (void)startDownload { NSOperationQueue *queue = [NSOperationQueue new]; NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadImage) object:nil]; [queue addOperation:operation]; [operation release]; [queue release]; } 

Then add loadImage, for example:

 - (void)loadImage { NSData *imageData = [[NSData alloc] initWithContents OfURL:[NSURL URLWithString:appRecord.imageURLString]]; self.apprecord.appIcon = [UIImage imageWithData:imageData]; [imageData release]; [self performSelectorOnMainThread:@selector(notifyMainThread) withObject:nil waitUntilDone:NO]; }
- (void)loadImage { NSData *imageData = [[NSData alloc] initWithContents OfURL:[NSURL URLWithString:appRecord.imageURLString]]; self.apprecord.appIcon = [UIImage imageWithData:imageData]; [imageData release]; [self performSelectorOnMainThread:@selector(notifyMainThread) withObject:nil waitUntilDone:NO]; } 

Then add notifyMainThread as follows:

 - (void)notifyMainThread { [delegate appImageDidLoad:self.indexPathInTableView]; }
- (void)notifyMainThread { [delegate appImageDidLoad:self.indexPathInTableView]; } 

Done! Launch it and you will see the exact behavior of the application store, no longer waiting for the request to download images until the scrolling stops, and will no longer wait for images to appear until the scrolling stops or until the user removes his finger from the screen .

Images are loaded as soon as the cell is ready for display, and the image is displayed immediately after its loading, period.

Sorry for any typos, I didn’t paste this from my application, I typed it, since now I am far from my mac ...

In any case, I hope this helps you all ...

+10
source

Check out the UIScrollViewDelegate . I implemented something like this by listening to scrollViewDidScroll: calculating the scroll speed (checking the contentOffset for the last recorded contentOffset divided by the time difference) and starting to load images as soon as the speed drops below a certain threshold. (You can also achieve something similar with UIScrollViewDelegate scrollViewDidEndDragging:willDecelerate: .

Of course, you do not need to check the speed; you can just load images UITableViewDelegate tableView:willDisplayCell:forRowAtIndexPath: whenever you see a new cell, but I found that if the user flips through tons of cells, you don't need to worry until you see that they will slow down to view.

0
source

All Articles