user1776683 user1776683 - 1 month ago 25
iOS Question

UIImageView in cell flicker when tableView reloadData

I know SDWebImage will clear memory cache when app enter background.
So, when app enter foreground, scroll tableView to refresh, and then reloadData, imageView in cell will flicker, even though data haven't changed.

[cell.toolsImageView sd_setImageWithURL:[NSURL URLWithString:toolsModel.iconUrl] placeholderImage:[UIImage imageNamed:@"morentu_circle"] options:SDWebImageRetryFailed];


In addition to judging model changes, is there any other way to avoid flicker?

Flicker steps around reloadData:


  1. show image before reloadData;

  2. reloadData and imageView reset image with URL;

  3. find image in SD memory cache, and not found;

  4. find image in SD disk cache, get it async;

  5. change to show placeholderImage;

  6. load disk image.


Answer
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock {
    [self sd_cancelCurrentImageLoad];

    NSURL *oldURL = [self sd_imageURL];

    objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    if (!(options & SDWebImageDelayPlaceholder) && (oldURL == nil || ![oldURL.absoluteString isEqualToString:url.absoluteString])) {
        dispatch_main_async_safe(^{
            self.image = placeholder;
        });
    }
    ......
}