From 737140d4cb718b1d6b8f2330f4e86e9943c5ec4f Mon Sep 17 00:00:00 2001 From: Luke Durrant Date: Wed, 20 May 2015 17:28:06 +1000 Subject: [PATCH 1/3] Added support for custom disk cache folder with fall back for caches directory --- SDWebImage/SDImageCache.h | 8 ++++++++ SDWebImage/SDImageCache.m | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/SDWebImage/SDImageCache.h b/SDWebImage/SDImageCache.h index 0dd05d10..e1009e56 100644 --- a/SDWebImage/SDImageCache.h +++ b/SDWebImage/SDImageCache.h @@ -76,6 +76,14 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot */ - (id)initWithNamespace:(NSString *)ns; +/** + * Init a new cache store with a specific namespace and directory + * + * @param ns The namespace to use for this cache store + * @param directory Directory to cache disk images in + */ +- (id)initWithNamespace:(NSString *)ns diskCacheDirectory:(NSString *)directory; + -(NSString *)makeDiskCachePath:(NSString*)fullNamespace; /** diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index c8dddf0f..af7bcd28 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -84,6 +84,11 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { } - (id)initWithNamespace:(NSString *)ns { + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); + return [self initWithNamespace:ns diskCacheDirectory:paths[0]]; +} + +- (id)initWithNamespace:(NSString *)ns diskCacheDirectory:(NSString *)directory { if ((self = [super init])) { NSString *fullNamespace = [@"com.hackemist.SDWebImageCache." stringByAppendingString:ns]; @@ -101,7 +106,15 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { _memCache.name = fullNamespace; // Init the disk cache - _diskCachePath = [self makeDiskCachePath:fullNamespace]; + if(directory!=nil) + { + _diskCachePath = [directory stringByAppendingPathComponent:fullNamespace]; + } + else + { + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); + _diskCachePath = [paths[0] stringByAppendingString:fullNamespace]; + } // Set decompression to YES _shouldDecompressImages = YES; From d7bc2c00367d5dba4ba3d4fff47e82dde279443a Mon Sep 17 00:00:00 2001 From: Luke Durrant Date: Wed, 20 May 2015 17:30:23 +1000 Subject: [PATCH 2/3] Improved by using makeDiskCachePath --- SDWebImage/SDImageCache.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index af7bcd28..26afdb33 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -84,8 +84,8 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { } - (id)initWithNamespace:(NSString *)ns { - NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); - return [self initWithNamespace:ns diskCacheDirectory:paths[0]]; + NSString *path = [self makeDiskCachePath:ns]; + return [self initWithNamespace:ns diskCacheDirectory:path]; } - (id)initWithNamespace:(NSString *)ns diskCacheDirectory:(NSString *)directory { @@ -112,8 +112,8 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { } else { - NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); - _diskCachePath = [paths[0] stringByAppendingString:fullNamespace]; + NSString *path = [self makeDiskCachePath:ns]; + _diskCachePath = path; } // Set decompression to YES From 33c13e79ca53e823b0b0474435206a63932ee2cc Mon Sep 17 00:00:00 2001 From: Bogdan Poplauschi Date: Mon, 13 Jul 2015 22:18:33 +0300 Subject: [PATCH 3/3] Coding style updates --- SDWebImage/SDImageCache.h | 2 +- SDWebImage/SDImageCache.m | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/SDWebImage/SDImageCache.h b/SDWebImage/SDImageCache.h index e1009e56..0157aaec 100644 --- a/SDWebImage/SDImageCache.h +++ b/SDWebImage/SDImageCache.h @@ -79,7 +79,7 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot /** * Init a new cache store with a specific namespace and directory * - * @param ns The namespace to use for this cache store + * @param ns The namespace to use for this cache store * @param directory Directory to cache disk images in */ - (id)initWithNamespace:(NSString *)ns diskCacheDirectory:(NSString *)directory; diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 26afdb33..03dfa3bd 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -106,12 +106,9 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { _memCache.name = fullNamespace; // Init the disk cache - if(directory!=nil) - { + if (directory != nil) { _diskCachePath = [directory stringByAppendingPathComponent:fullNamespace]; - } - else - { + } else { NSString *path = [self makeDiskCachePath:ns]; _diskCachePath = path; }