【问题标题】:iOS - SDWebImage loading wrong ImageiOS - SDWebImage 加载错误的图像
【发布时间】:2014-05-29 06:23:00
【问题描述】:

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

细节场景

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

但有时在 Imageview 中加载的图片会有不同的图片。

让我们说

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

        }

【问题讨论】:

    标签: ios uiimageview sdwebimage image-caching


    【解决方案1】:

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

    改成这样:

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

    【讨论】:

    • 感谢 Mithun。但是你能提供你建议的方式和方式的原因或不同之处吗?我犯了什么错误?
    • 您的方法的不同之处在于,您只是下载图像并处理结果,而无需检查UIImageView 的图像 URL 在您开始下载图像后是否实际发生了变化。 Midhun 使用UIImageView 类别方法,它在内部处理下载以及做一些不错的事情,例如在设置图像之前检查加载的 URL 是否真的正确。
    【解决方案2】:

    在实现了 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
                }
    
            }];
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      相关资源
      最近更新 更多