我正在使用 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
单元格的拇指设置:3
单元格设置:0
单元格真实图像集:0
似乎不知何故,某些下载被取消了。
最佳答案
我最终使用了两个 UIImageView,每张照片一个,重叠。当真实(大)图像完全下载后,我会平滑地淡化模糊图像。
关于ios - SDWebImage 库 - 来自 URL 的占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30823654/