From df751e737b0236c942a3f9e10f63c968b2d54fdd Mon Sep 17 00:00:00 2001 From: mythodeia Date: Wed, 15 Jul 2015 11:35:03 +0300 Subject: [PATCH] added option to disable memory cache use this: ```shouldDisableMemoryCache``` to toggle memory cache --- SDWebImage/SDImageCache.h | 5 +++++ SDWebImage/SDImageCache.m | 24 ++++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/SDWebImage/SDImageCache.h b/SDWebImage/SDImageCache.h index ca6033a5..2bbb902a 100644 --- a/SDWebImage/SDImageCache.h +++ b/SDWebImage/SDImageCache.h @@ -47,6 +47,11 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot */ @property (assign, nonatomic) BOOL shouldDisableiCloud; +/** + * use memory cache [defaults to YES] + */ +@property (assign, nonatomic) BOOL shouldCacheImagesInMemory; + /** * The maximum "total cost" of the in-memory image cache. The cost function is the number of pixels held in memory. */ diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index c197b5c3..f5ab18e9 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -116,6 +116,9 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { // Set decompression to YES _shouldDecompressImages = YES; + // memory cache enabled + _shouldCacheImagesInMemory = YES; + // Disable iCloud _shouldDisableiCloud = YES; @@ -196,9 +199,11 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { if (!image || !key) { return; } - - NSUInteger cost = SDCacheCostForImage(image); - [self.memCache setObject:image forKey:key cost:cost]; + // if memory cache is enabled + if (self.shouldCacheImagesInMemory) { + NSUInteger cost = SDCacheCostForImage(image); + [self.memCache setObject:image forKey:key cost:cost]; + } if (toDisk) { dispatch_async(self.ioQueue, ^{ @@ -290,6 +295,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { } - (UIImage *)imageFromDiskCacheForKey:(NSString *)key { + // First check the in-memory cache... UIImage *image = [self imageFromMemoryCacheForKey:key]; if (image) { @@ -298,7 +304,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { // Second check the disk cache... UIImage *diskImage = [self diskImageForKey:key]; - if (diskImage) { + if (diskImage && self.shouldCacheImagesInMemory) { NSUInteger cost = SDCacheCostForImage(diskImage); [self.memCache setObject:diskImage forKey:key cost:cost]; } @@ -369,7 +375,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { @autoreleasepool { UIImage *diskImage = [self diskImageForKey:key]; - if (diskImage) { + if (diskImage && self.shouldCacheImagesInMemory) { NSUInteger cost = SDCacheCostForImage(diskImage); [self.memCache setObject:diskImage forKey:key cost:cost]; } @@ -400,9 +406,11 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { if (key == nil) { return; } - - [self.memCache removeObjectForKey:key]; - + + if (self.shouldCacheImagesInMemory) { + [self.memCache removeObjectForKey:key]; + } + if (fromDisk) { dispatch_async(self.ioQueue, ^{ [_fileManager removeItemAtPath:[self defaultCachePathForKey:key] error:nil];