Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 827
AgnosticDev Avatar asked Feb 15 '13 20:02

AgnosticDev


People also ask

How do I cache an image in Swift?

Internally, NSURLCache uses both in-memory and on-disk image caching, and it makes its decisions based on the size of the data. All you need to do is to initialise this cache and to set it as a default for your application, and then use NSURLSession to download your images/data.

How do I use SDWebImage in Swift 4?

Click File -> Swift Packages -> Add Package Dependency , enter SDWebImage repo's URL. Or you can login Xcode with your GitHub account and just type SDWebImage to search. After select the package, you can choose the dependency type (tagged version, branch or commit). Then Xcode will setup all the stuff for you.


1 Answers

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?

like image 190
Stakenborg Avatar answered Sep 18 '22 02:09

Stakenborg