【问题标题】:SDWebImage Library - placeholder from URLSDWebImage 库 - 来自 URL 的占位符
【发布时间】:2016-06-15 21:11:38
【问题描述】:

我正在使用 SDWebImage 异步下载和缓存 UITableView 中的图像,但我遇到了一些问题。

场景如下: 创建单元格时,我想在真实图像进入之前从 URL 加载低质量的模糊图像(1-2kb)。下载更高质量的图像后,我想显示那个。 到目前为止,我已经尝试了这些选项,但似乎都没有按我预期的方式工作:

1:

    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadImageWithURL:[NSURL URLWithString:lowQualityImageURL]
                          options:0
                         progress:^(NSInteger receivedSize, NSInteger expectedSize) {}
                        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {

                            if (finished) {
                                cell.pictureView.image = image;
                                [manager downloadImageWithURL:[NSURL URLWithString:highQualityImageURL]
                                                      options:0
                                                     progress:^(NSInteger receivedSize, NSInteger expectedSize) {}
                                                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                                                        if (image && finished) {
                                                            [UIView transitionWithView:cell.pictureView
                                                                              duration:0.1f
                                                                               options:UIViewAnimationOptionTransitionCrossDissolve
                                                                            animations:^{
                                                                                cell.pictureView.image = image;
                                                                            } completion:^(BOOL finished) {

                                                                            }];
                                                        }
                                                    }];
                            }
                        }];

当使用此代码时,低质量的图像似乎在真实图像之前被下载并显示,但如果用户在表格中快速滚动,他最终会得到一些单元格的错误图像(因为单元格重用 -我猜)。 - 这里有什么解决办法吗?

2:

[cell.pictureView sd_setImageWithURL:[NSURL URLWithString:lowQualityImageURL] placeholderImage:[UIImage new] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [cell.pictureView sd_setImageWithURL:[NSURL URLWithString:highQualityImageURL] placeholderImage:image options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

            }];
    }];

这种方法似乎解决了“细胞图像错误”的问题,但大多数细胞最终会显示其相应的模糊图像,而不是应该显示的高质量图像。

所以,总而言之,您对如何实现我想要的结果有什么建议吗? (下载低质量图像 -> 显示直到下载高质量图像 -> 将其替换为高质量图像)

LE:使用第二种方法时,我看到了一种奇怪的行为。控制台输出:

  1. 为单元格设置的拇指:1

  2. 为单元格设置的真实图像:1

  3. 为单元格设置的拇指:2

  4. 为单元格设置的拇指:3

  5. 为单元格设置的拇指:0

  6. 为单元格设置的真实图像:0

似乎某些下载被取消了。

【问题讨论】:

    标签: ios image uitableview asynchronous sdwebimage


    【解决方案1】:

    我最终使用了两个 UIImageView,每张照片一个,重叠。当真正的(大)图像完全下载后,我会平滑地淡化模糊的图像。

    【讨论】:

      【解决方案2】:

      这是我在 Swift 中使用 Nuke 编写的辅助函数:

      func loadImageWithTwoQualities(lowQualityURL: NSURL, highQualityURL: NSURL, completion: (UIImage) -> () ) {
      
          Nuke.taskWith(lowQualityURL) {
              completion($0.image!)
      
              Nuke.taskWith(highQualityURL) {
                  completion($0.image!)
              }.resume()
      
          }.resume()
      }
      

      您可以将它与任何类型的图像视图一起使用,如下所示:

      let normalImageUrl = NSURL(string: picture)!
      let largeImageUrl = NSURL(string: picture + "?type=large")!
      
      loadImageWithTwoQualities(normalImageUrl, highQualityURL: largeImageUrl) { image in
           self.someUIButton.setBackgroundImage(image, forState: .Normal)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-05
        • 1970-01-01
        • 2018-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多