From 737140d4cb718b1d6b8f2330f4e86e9943c5ec4f Mon Sep 17 00:00:00 2001 From: Luke Durrant Date: Wed, 20 May 2015 17:28:06 +1000 Subject: [PATCH] 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 0dd05d10c..e1009e56c 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 c8dddf0f7..af7bcd28d 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -84,6 +84,11 @@ - (id)init { } - (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 @@ - (id)initWithNamespace:(NSString *)ns { _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;