1

我正在使用 SDWebImage 库进行缓存和延迟加载。但我发现有时它会显示另一个单元格的图像。

细节场景

  • 有 CollectionView 具有包含 UIImageView 和标签的单元格。
  • ImageView 包含用户的图像和带有他们
    名称的标签。

但有时 Imageview 中加载的 Image 会有不同的图像。

让我们说

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

所以这里因为索引有nameD所以图像应该是b“D”但它显示的是nameB的图像,即“B”

这是我使用的代码

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

        }
4

2 回答 2

3

您的方法的问题是,如果您滚动或完全下载图像时,单元格变量将保存任何其他单元格的地址,而不是您要显示图像的实际单元格。这就是图像显示错误的原因。

像这样改变它:

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"); }
     ];
}
于 2014-05-29T06:42:57.680 回答
0

实现 Midhun 的答案后,我根据SDWebImage 的新库找到了另一种方法。因为新的UIImageView+WebCache.h中没有成功/失败块

所以这对我有用。

    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
            }

        }];

    }
于 2014-05-29T09:20:43.027 回答