【发布时间】: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
为单元格设置的拇指:3
为单元格设置的拇指:0
为单元格设置的真实图像:0
似乎某些下载被取消了。
【问题讨论】:
标签: ios image uitableview asynchronous sdwebimage