AgnosticDev AgnosticDev - 1 year ago 193
Objective-C Question

SDWebImage Download image and store to cache for key

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) {

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.

Answer Source

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.

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.

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?

Hope this helps,


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download