Asynchronous UITableViewCell Image Loading Using GCD

江枫思渺然 提交于 2019-12-02 23:01:44
Carl Veazey

You should call setNeedsLayout on the cell after setting the thumbnail.

From the Apple Doc:

"Call this method on your application’s main thread when you want to adjust the layout of a view’s subviews. This method makes a note of the request and returns immediately. Because this method does not force an immediate update, but instead waits for the next update cycle, you can use it to invalidate the layout of multiple views before any of those views are updated. This behavior allows you to consolidate all of your layout updates to one update cycle, which is usually better for performance."

Another solution (and the one that I used for that assignment) is to call setNeedsDisplay on on the cell's imageView once you have assigned the UIImage to that imageView.

So basically, I created a class called FlickrDownloader that wraps up the functionality of FlickrFetcher (as well as my caching code). FlickrDownloader has this method:

(void)getImageForPhotoInfo:(NSDictionary *)photoInfo ofFormat:(FlickrPhotoFormat)format withCallback:(image_setter_callback_t)callback;

This is basically a call to the same function of FlickrFetcher, but it adds a callback thats is called upon the completion of downloading the photo. In the case of updating the imageView of a UITableViewCell the callback looks like this:

image_setter_callback_t setImage = ^(UIImage *image){
    cell.imageView.image = image;
    [cell.imageView setNeedsDisplay];
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!