开发者

UITableViewCell load Images

I have 16 locally stored pre-rendered 40x40 px images that I want to use as my UITableViewCell images. I can get it to d开发者_JS百科isplay them fine using [UIImage imageNamed:]; but I have noticed this slows down the table view scrolling.

I have looked through the apple example for lazy loading etc. and understand I need to do it on a background thread or asynchronously, but have not found an example for local images.

Can anybody help?


Subclass the UITableView cell, and create a new method in the cell that is something like:

- (void)setImage:(NSString *)imageName;

Then from where you used to set the image, change it to instead do something like:

[cell setImage:nil];
[cell performSelector:@selector(setImage:) withObject:@"imageName" afterDelay:0];

Changing it to performSelector:withObject:afterDelay: means that the code to set the image will not execute while the tableView is scrolling. This should lead to a significant improvement in scroll performance as it will not set images into cells until the scrolling stops.

However you will notice the lack of images while scrolling so you may want to consider a small placeholder image in the images place to appear while scrolling.

Personally I use this method conditionally in my code. I detect how old the UIDevice is, and if its an older generation device I will lazy load like this to increase scroll performance.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜