Thursday, November 28, 2013

http://stackoverflow.com/questions/14902835/sdwebimage-download-image-and-store-to-cache-for-key



Hello I am using the SDWebImage framework in a project and I was wanting to download and cache some images, but I think my code is storing the image in the cache twice? Is there a way to store a UIImage in the cache by a key one time? 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];              }              }];  

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

asked Feb 15 at 20:29
AgnosticDev
754314
add comment

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,

-Brandon

answered Apr 22 at 19:04
Stakenborg
636214

No comments:

Post a Comment