SDWebImage UITableView单元格图像不存在 [英] SDWebImage UITableView Cell Images Not Persisting

查看:53
本文介绍了SDWebImage UITableView单元格图像不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SDWebImage库以异步方式从Web服务下载和缓存图像.以下是我用来下载图像的方法:

I am using SDWebImage library in order to download and cache images and from a web service asynchronously. Following is the method i use to download the images:

- (void) downloadThumbnails:(NSURL *)finalUrl
{
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadWithURL:finalUrl
                     options:0
                    progress:nil
                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
     {
         if (image)
         {
             [self setThumbnail:image];
         }
     }];

}

它已成功从Web服务下载图像并将其显示在表格视图中.但是,它不会在应用程序运行之间保留映像.为了缓存图像,我还有什么需要做的吗?

It is successfully downloading the images from the web service and showing them in the tableview. However its not persisting the images between application runs. Is there anything else i need to do in order to cache the images??

更新:请不要使用以下内容:

UPDATE: KINDLY DO NOT suggest to use the following:

[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

因为,我的程序的结构不同.其次,如果您阅读了SDWebImage库的描述 HERE ,您将知道它可以用于我使用SDWebImageManager使用它的方式.

Because, the structure of my program is different. Secondly, if you read description of the SDWebImage library HERE, you will get to know that it can be used the way i am using it using SDWebImageManager.

推荐答案

我怀疑在这种特定情况下SDWebImage中的缓存机制无法正常工作的原因是因为它基于图像URL.

I suspect that the reason why the cache mechanism in SDWebImage is not working correctly in this specific case is because it is based on the image URL.

因此,如果您随后下载了一个 image 并将其分配给对象的某些属性(例如您正在将其分配给 thumbnail ),属性将仅绕过缓存机制.

So, if you just download an image and assign it to some of your object's property (like you are doing assigning it to thumbnail), if you later access that property if will simply walk around the cache mechanism.

您可以采取以下措施来保持程序的结构:

What you could do to keep the structure of your program is:

  1. thumbnailURL 存储在您的 self 对象中;

当您第一次下载图像时,在执行操作时调用 SDWebImageManager downloadWithURL:方法;这也将图像存储在缓存中;(您已经按照上面的代码执行了此操作)

when you download the image the first time, call SDWebImageManager downloadWithURL: method as you are doing; this will also store the image in the cache; (this you are already doing as per your code above)

thumbnail 设为只读属性,该属性访问缓存以检索与 thumbnailURL 相关的图像.

make thumbnail a read-only property that accesses the cache retrieving the image associated to thumbnailURL.

这是您应做的最少更改.

This is the least amount of changes you should do.

这可能是第3点所述的只读属性的实现.

This could be an implementation of the read-only property described at point 3:

- (UIImage*)thumbnail {

    return [[SDImageCache sharedCache] imageFromDiskCacheForKey:self.thumbnailURL];
}

其中 self.thumbnailURL 是对象中保存图像URL的另一个属性.

where self.thumbnailURL is another property in your object where you save the image URL.

希望这会有所帮助.

这篇关于SDWebImage UITableView单元格图像不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆