SDWebImage 库 - 来自 URL 的占位符

SDWebImage Library - placeholder from URL

我正在使用 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) {

                                                                            }];
                                                        }
                                                    }];
                            }
                        }];

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

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) {

            }];
    }];

这种方法似乎解决了 "wrong image for cell" 问题,但大多数单元格最终显示的是它们相应的模糊图像,而不是应该显示的高质量图像。

所以,总而言之,您对我如何才能达到我想要的结果有什么建议吗? (下载低质量图片 -> 显示它直到下载高质量图像 -> 用高质量图像替换它)

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

  1. 单元格的缩略图设置:1

  2. 单元格的真实图像集:1

  3. 单元格的缩略图设置:2

  4. 单元格的缩略图设置:3

  5. 单元格的缩略图设置:0

  6. 单元格的真实图像集:0

似乎有些下载被取消了。

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

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

func loadImageWithTwoQualities(lowQualityURL: NSURL, highQualityURL: NSURL, completion: (UIImage) -> () ) {

    Nuke.taskWith(lowQualityURL) {
        completion([=10=].image!)

        Nuke.taskWith(highQualityURL) {
            completion([=10=].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)
}