我正在使用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:使用第二种方法时,我会看到奇怪的行为。控制台输出:
为单元格设置的
看来,某些下载被取消了。
最佳答案
我最终使用了两个UIImageViews,每张照片一个,重叠了。完全下载完真实的(大)图像后,我会平滑淡化模糊的图像。
关于ios - SDWebImage库-网址中的占位符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30823654/