如何使用 SDWebImage 为多个尺寸的图像缓存图像
How to use SDWebImage to cache image for multiple sized images
从我的网络服务中,我得到了多种尺寸的图像。我正在将它们加载到 TableView 中。当我加载它们时,尚未缓存的新图像在占位符和原始图像之间闪烁,或者有时图像看起来比通常的尺寸小。但只需向下或向上滚动一点点就可以解决问题,但我希望它们从一开始就保持原始大小。但是我想下次它会以原始形状出现,因为图片已经缓存了
最初
稍微滚动一下后
我在 cellForRowAtIndexPath 中的代码:
[cell.image sd_setImageWithURL:[NSURL URLWithString:[NSMutableString stringWithFormat:@"%@app/media/access/pictures?p=%@",baseurl,data.picPath]]
placeholderImage:[UIImage imageNamed: @"image_loader.gif"]];
然后在 heightForRowAtIndexPath:
NSURL *filePath = [NSURL URLWithString:[NSMutableString stringWithFormat:@"%@app/media/access/pictures?p=%@",baseurl,data.picPath]];
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:filePath];
UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
self.img = image;
if (self.img.size.width > CGRectGetWidth(self.view.bounds)) {
CGFloat ratio = self.img.size.height / self.img.size.width;
return CGRectGetWidth(self.view.bounds) * ratio+140;
} else {
return self.img.size.height+180;
}
我访问了 https://github.com/rs/SDWebImage,在他们的常见问题部分他们提到了这个问题,但建议的解决方案对我不起作用!!!
实际上我做了什么,我检查了图像是否已经被缓存,如果没有我已经在 heightForRowAtIndexPath 下异步下载它:
if(image != NULL)
{
self.img = image;
}
else
{
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:filePath
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (image && finished) {
// do something with image
self.img = image;
}
}];
}
从我的网络服务中,我得到了多种尺寸的图像。我正在将它们加载到 TableView 中。当我加载它们时,尚未缓存的新图像在占位符和原始图像之间闪烁,或者有时图像看起来比通常的尺寸小。但只需向下或向上滚动一点点就可以解决问题,但我希望它们从一开始就保持原始大小。但是我想下次它会以原始形状出现,因为图片已经缓存了
最初
稍微滚动一下后
我在 cellForRowAtIndexPath 中的代码:
[cell.image sd_setImageWithURL:[NSURL URLWithString:[NSMutableString stringWithFormat:@"%@app/media/access/pictures?p=%@",baseurl,data.picPath]]
placeholderImage:[UIImage imageNamed: @"image_loader.gif"]];
然后在 heightForRowAtIndexPath:
NSURL *filePath = [NSURL URLWithString:[NSMutableString stringWithFormat:@"%@app/media/access/pictures?p=%@",baseurl,data.picPath]];
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:filePath];
UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
self.img = image;
if (self.img.size.width > CGRectGetWidth(self.view.bounds)) {
CGFloat ratio = self.img.size.height / self.img.size.width;
return CGRectGetWidth(self.view.bounds) * ratio+140;
} else {
return self.img.size.height+180;
}
我访问了 https://github.com/rs/SDWebImage,在他们的常见问题部分他们提到了这个问题,但建议的解决方案对我不起作用!!!
实际上我做了什么,我检查了图像是否已经被缓存,如果没有我已经在 heightForRowAtIndexPath 下异步下载它:
if(image != NULL)
{
self.img = image;
}
else
{
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:filePath
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (image && finished) {
// do something with image
self.img = image;
}
}];
}