SDWebImage下载图像并存储到密钥缓存 [英] SDWebImage Download image and store to cache for key

查看:103
本文介绍了SDWebImage下载图像并存储到密钥缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在一个项目中使用SDWebImage框架,我想下载并缓存图像,但是我认为我的代码两次将图像存储在缓存中?有什么方法只能通过一次密钥将图像存储在缓存中吗?这是我的代码.

Hello I am using the SDWebImage framework in a project and I want to download and cache images, but I think my code is storing an image in the cache twice? Is there any way to store the image in the cache by a key only once? Here is my code.

         SDWebImageManager *manager = [SDWebImageManager sharedManager];
         [manager downloadWithURL:[NSURL URLWithString:url] options:0 progress:^(NSUInteger receivedSize, long long expectedSize) {

          } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {

            if(image){
                NSString *localKey = [NSString stringWithFormat:@"Item-%d", i];
                [[SDImageCache sharedImageCache] storeImage:image forKey:localKey];
            }

          }];  

有什么我想念的吗?似乎在我的分配工具中这样做会占用大量内存.

Is there something that I missed? Looks like doing this in my allocations instrument is pilling up a lot of memory.

推荐答案

我很惊讶没有人回答这个问题,但是我也遇到了类似的问题并遇到了这个问题,因此我将为查看此问题的人们回答向前(假设您现在已经自己解决了这个问题).

I'm surprised nobody answered this question, but I've had a similar question and came across this, so I'll answer it for people viewing this going forward (assuming you've sorted this out yourself by now).

要直接回答您的问题,是的,您将图像缓存了两次.

To directly answer your question, yes, you are caching the image twice.

下载到SDWebImageManager的调用会自动基于图像URL的absoluteString用键缓存图像.如果您需要自己的密钥,则可以在SDWebImageDownloader上使用下载调用,据我所知,默认情况下不会进行缓存.从那里,您可以像正在做的那样调用sharedImageCache并使用所需的任何键进行缓存.

Download calls to SDWebImageManager automatically cache images with keys based on the absoluteString of the image's url. If you want your own key, you can use the download call on SDWebImageDownloader which as far as I can tell does NOT cache by default. From there you can call the sharedImageCache as you're already doing and cache with whatever key you want.

此外,奇怪的是,在任何情况下您都看到分配堆积,因为SDWebImage喜欢缓存到磁盘而不是通常缓存到内存.也许同时有其他事情发生?

That aside, it is strange you're seeing allocations piling up in any case as SDWebImage likes to cache to disk and not memory generally. Maybe something else is going on at the same time?

希望这会有所帮助,

-布兰登

这篇关于SDWebImage下载图像并存储到密钥缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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