iosimagecachingafnetworkingafnetworking-2

White flash when setImageWithUrl displays a cached image


Using AFNetworking 2.0's method setImageWithUrl, I set an image in an imageView located in a UITableViewCell. It works fine when the image displayed is first downloaded and then set. If, however, the image is available locally (has been cached) when it's set, there's a quick white flash before it is displayed.

Do you know how to avoid this?

Steps to reproduce:

  1. Set image (image will be cached)
  2. Close application
  3. Start application
  4. Set (the now cached) image

Code for setting the image:

- (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;  
}

Solution

  • If anyone's bumping into the same issue, here's how I solved it:

    In the success block, replace

    cell.myImageView.image = image;
    

    with

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

    VoilĂ , no more ugly flashes!

    Credits to this answer for leading me to the right track.