开发者

UIImage in uitableViewcell slowdowns scrolling table

开发者 https://www.devze.com 2023-03-02 10:20 出处:网络
I am loading image with data in my table view. Images are coming from web. I created method to get image url in model class.Model class has Nsdictionary and objects.

I am loading image with data in my table view. Images are coming from web. I created method to get image url in model class.Model class has Nsdictionary and objects. But this images is slowing down scrolling .Here is my code

UIImage *image = [UIImage imageWithData开发者_开发知识库:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",
                                                                                        [(Tweet *)[recentTweets objectAtIndex:indexPath.row]urlString]]]]];
cell.imageView.image = image;

Please tell Where I am going wrong?


Use lazy loading and do your own drawing. Try to understand the techniques on the sample projects I linked. These are the best ways to improve the performance of tables with images.


here is the methodology I use for loading images into a UITableView from a remote location:

in your .h file, declare a mutable dictionary for storing the images:

 NSMutableDictionary *images; 

initialize the dictionary in -init... or in -viewDidLoad

images = [[NSMutableDictionary alloc]init];

in the .m, tableView:cellForRowAtIndexPath:, see if the image exists in your dictionary for the indexPath

UIImage *img = [images objectForKey: indexPath];

if the image does exist, just set it.

if (img) cell.imageView.image = img;

if the image does NOT exist, set the cell's image to a temporary image...

if (!img) cell.imageView.image = [UIImage imageNamed:@"imageUnavailable.png"];

AND add that image to your dictionary so it doesnt try to refetch the image if you scroll off and back to that image before it loads...

[images setObject:[UIImage imageNamed:@"imageUnvailable.png"] forKey: indexPath];

then, in this same block, use an NSOperationQueue, and a custom NSOperation ( here is a reference - NSOperation and SetImage) to get your image and call back into your UITableViewController with that image.

in your callback, add your image to the dictionary (overwriting the temp image) and call [tableView reloadData]

this will give you a nice non blocking user experience.


The are a couple of ways how to do it. I had the best experience with a Queue for the HttpRequests, which I pause during the scrolling process.

I highly recommend this framework for that: http://allseeing-i.com/ASIHTTPRequest/

I also implemented an image cache, which only loads the images if there weren't in the cache.

And the last tweak was to actually draw the images instead of using a high level uicomponent like the UIImageView


The number one reason your code is slow right now is because you're making a network call on the main thread. This blocks an UI from being updated and prevents the OS from handling events (such as taps).

As already suggested, I recommend ASIHTTPRequest. Combine asynchronous requests with an NSOperationQueue with a smaller concurrency count (say, 2) for the image requests, caching, and reloading the rows when images come in (on the main thread) (also only reloading the row if its currently visible). You can get a smooth scrolling table view.

I have an example of this on github here: https://github.com/subdigital/iphonedevcon-boston

It's in the Effective Network Programming project and includes a set of projects that progressively improve the experience.


Download the images before you load the tableView, and store them in an NSArray. Then when the cellForRowAtIndexPath method is called, show a loading image until the image is in the array.

0

精彩评论

暂无评论...
验证码 换一张
取 消