setImageWithUrl显示缓存图像时出现白色闪烁

Lev*_*rin 1 caching image ios afnetworking afnetworking-2

使用AFNetworking 2.0的方法setImageWithUrl,我在位于UITableViewCell的imageView中设置了一个图像.首次下载然后设置显示的图像时,它可以正常工作.但是,如果图像在设置时在本地可用(已缓存),则在显示之前会有快速白色闪烁.

你知道怎么避免这个吗?

重现步骤:

  1. 设置图像(图像将被缓存)
  2. 关闭申请
  3. 开始申请
  4. 设置(现在缓存)图像

设置图像的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    myCell *cell = (myCell *)[tableView dequeueReusableCellWithIdentifier:@"imageCell"];

    [cell.myImageView setImageWithURLRequest:[NSURLRequest requestWithURL:myImageUrl] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        cell.myImageView.image = image;

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
        NSLog(@"Failed");
    }];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;  
}
Run Code Online (Sandbox Code Playgroud)

Lev*_*rin 7

如果有人碰到同样的问题,我就是这样解决的:

在成功块中,替换

cell.myImageView.image = image;
Run Code Online (Sandbox Code Playgroud)

if (request) {
    [UIView transitionWithView:cell.myImageView
                      duration:0.8f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{[cell.myImageView setImage:image];}
                    completion:NULL];
}else{
    [cell.myImageView setImageWithURL:myImageURL];
}
Run Code Online (Sandbox Code Playgroud)

Voilà,不再是丑陋的闪光!

归功于这个答案,引领我走上正轨.