首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SDWebImage库-来自URL的占位符

SDWebImage库-来自URL的占位符
EN

Stack Overflow用户
提问于 2015-06-13 21:20:35
回答 2查看 835关注 0票数 2

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

场景如下:当创建单元时,我希望在真正的图像出现之前,从URL加载一个低质量的模糊图像(1-2kb)。下载了更高质量的图片后,我想展示一下。到目前为止,我已经尝试过这些选择,但似乎都没有像我所期望的那样工作:

1:

代码语言:javascript
复制
    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:

代码语言:javascript
复制
[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) {

            }];
    }];

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

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

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

  1. 为单元格设置拇指:1
  2. 单元格的真实图像集:1
  3. 用于细胞的拇指设置:2
  4. 用于细胞的拇指设置:3
  5. 单元格的拇指设置:0
  6. 单元格的真实图像集:0

似乎有些下载被取消了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-17 17:52:53

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

票数 2
EN

Stack Overflow用户

发布于 2016-06-15 13:11:38

下面是我用Swift使用核弹编写的一个帮助函数

代码语言:javascript
复制
func loadImageWithTwoQualities(lowQualityURL: NSURL, highQualityURL: NSURL, completion: (UIImage) -> () ) {

    Nuke.taskWith(lowQualityURL) {
        completion($0.image!)

        Nuke.taskWith(highQualityURL) {
            completion($0.image!)
        }.resume()

    }.resume()
}

您可以在任何类型的图像视图中使用它,如下所示:

代码语言:javascript
复制
let normalImageUrl = NSURL(string: picture)!
let largeImageUrl = NSURL(string: picture + "?type=large")!

loadImageWithTwoQualities(normalImageUrl, highQualityURL: largeImageUrl) { image in
     self.someUIButton.setBackgroundImage(image, forState: .Normal)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30823654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档