Handling download of image using SDWebImage while reusing UITableViewCell

前端 未结 3 838
眼角桃花
眼角桃花 2021-02-01 10:32

I have used third party library SDWebImage to download image for my UITableView cells, UIImageView is created within cell and fired request while configuring cell like this.

3条回答
  •  自闭症患者
    2021-02-01 11:24

    The line that causes the download to get cancelled is in UIImageView+WebCache.m

    The first line in - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock

    calls [self cancelCurrentImageLoad];. If you get rid of this line, the download operations should continue on going.

    The problem at this point will be that there will be a race condition if a single UIImageView is waiting for multiple downloads to complete. The last download to complete may not necessarily be the last one started, so you may end up with the wrong image in your cell.

    There are a few ways to go about fixing this. The easiest might be just to add another associated object in UIImageView+WebCache, simply the last loaded URL. Then, you can check this URL in the completion block and only set the image if it matches.

提交回复
热议问题