Search code examples
iosuiimageviewsdwebimageimage-caching

iOS - SDWebImage loading wrong Image


I am using SDWebImage Library for caching and Lazy loading. But I found that sometime it is showing Image of another cell.

Detail Scenerio

  • There is CollectionView having cells containing UIImageView and Labels.
  • ImageView contains the Image of Users and label with their
    names.

But sometimes The Image loaded in the Imageview Have the different image.

Lets say

Index  Name  Image
0      nameA  A
1      nameB  B
2      nameC  C
3      nameD  B

So here As at index have nameD so image should b "D" but it is displaying Image of nameB i.e. "B"

This is the Code i used

      if ([aMutDict objectForKey:@"picture_url"])
        {
            [[SDWebImageManager sharedManager]downloadWithURL:[NSURL URLWithString:[aMutDict objectForKey:@"picture_url"]] options:SDWebImageProgressiveDownload progress:Nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
                if(image){
                    [cell.imgProfilePic setImage:image];
                }else{
                    [cell.imgProfilePic setImage:ApplicationDelegate.gblImgPlaceholder];
                }
            }];

        }

Solution

  • The problem with your approach is, if you scroll or when the image is downloaded completely the cell variable will hold the address of any other cell not the actual cell you want to display the image. That's why the image displayed as wrong.

    Change it like:

    if ([aMutDict objectForKey:@"picture_url"])
    {
        [cell.imgProfilePic setImageWithURL:[NSURL URLWithString:[aMutDict objectForKey:@"picture_url"]] 
                        placeholderImage:ApplicationDelegate.gblImgPlaceholder 
                        success:^(UIImage *image) {
                             NSLog("Image Loaded");
                         }
                         failure:^(NSError *error) {
                             NSLog("Image Not Loaded"); }
         ];
    }