1

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

        }
Mrug
  • 4,963
  • 2
  • 31
  • 53

2 Answers2

3

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"); }
     ];
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • thanks Mithun. but can you provide reason or the difference in the way that you are suggesting and the way done.What mistake i have made ? – Mrug May 29 '14 at 06:54
  • The difference in your approach is that you are simply downloading the image and handling the result without checking to see if the image URL for that `UIImageView` has actually changed since you started downloading the image. Midhun is using the `UIImageView` category method, which handles the downloading internally along with doing a few nice things such as checking to see if the loaded URL is actually correct before setting the image. – Dima May 29 '14 at 07:15
0

After implementing the Answer of Midhun, I found another Method as per New library of SDWebImage. Because success/failure block is not there in new UIImageView+WebCache.h

So this works for me.

    if ([aMutDict objectForKey:@"picture_url"])
    {
        [cell.imgProfilePic setImageWithURL:[NSURL URLWithString:[aMutDict objectForKey:@"picture_url"]] placeholderImage:ApplicationDelegate.gblImgPlaceholder options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
            if(error){
                //Image Not Loaded
            }
            if(image)
            { 
                // Image Loaded
            }

        }];

    }
Mrug
  • 4,963
  • 2
  • 31
  • 53