我使用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) {
}];
}];这种方法似乎解决了“错误的细胞图像”问题,但大多数细胞最终显示的是相应的模糊图像,而不是本应显示的高质量图像。
所以,总括而言,你对我如何达到我想要的结果有什么建议吗?(下载低质量的图像->,直到下载高质量的图像,->将其替换为高质量的图像)
莱:当我使用第二种方法时,我看到了一种奇怪的行为。控制台输出:
似乎有些下载被取消了。
发布于 2015-06-17 17:52:53
最后,我使用了两个UIImageViews,每张照片一张,重叠。当真正的(大的)图像被完全下载时,我平滑地淡出了模糊的图像。
发布于 2016-06-15 13:11:38
下面是我用Swift使用核弹编写的一个帮助函数
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)
}https://stackoverflow.com/questions/30823654
复制相似问题