Skip to content

Commit 447f1cc

Browse files
SRandazzoOlivier Poitrey
authored and
Olivier Poitrey
committed
Custom image cache search paths
This is particularly useful if you are bundling images with your app that have been cached by SDWebImage. (ie. if you are 'seeding' your app with a core-data file that contains a lot of URL's to images and would like to also seed those images without having to copy every one of them over) For example, you can tell SDImageCache to add '[[NSBundle mainBundle] resourcePath]' as a custom path, so that the main bundle will be queried for cached images. This prevents the need for you to copy pre-cached images over to the caches/ImageCache folder that SDImageCache normally checks for. The custom paths are read-only.
1 parent d38e13c commit 447f1cc

File tree

5 files changed

+69
-6
lines changed

5 files changed

+69
-6
lines changed
Binary file not shown.

Examples/SDWebImage Demo.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
3E75A9861742DBE700DA412D /* CustomPathImages in Resources */ = {isa = PBXBuildFile; fileRef = 3E75A9851742DBE700DA412D /* CustomPathImages */; };
1011
531041C1157EAC8F00BBABC3 /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 537612E6155ABA44005750A4 /* ImageIO.framework */; };
1112
5376129A155AB74D005750A4 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 53761299155AB74D005750A4 /* UIKit.framework */; };
1213
5376129C155AB74D005750A4 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5376129B155AB74D005750A4 /* Foundation.framework */; };
@@ -48,6 +49,7 @@
4849
/* End PBXContainerItemProxy section */
4950

5051
/* Begin PBXFileReference section */
52+
3E75A9851742DBE700DA412D /* CustomPathImages */ = {isa = PBXFileReference; lastKnownFileType = folder; path = CustomPathImages; sourceTree = SOURCE_ROOT; };
5153
53761295155AB74D005750A4 /* SDWebImage Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "SDWebImage Demo.app"; sourceTree = BUILT_PRODUCTS_DIR; };
5254
53761299155AB74D005750A4 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
5355
5376129B155AB74D005750A4 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -120,6 +122,7 @@
120122
5376129F155AB74D005750A4 /* SDWebImage Demo */ = {
121123
isa = PBXGroup;
122124
children = (
125+
3E75A9851742DBE700DA412D /* CustomPathImages */,
123126
537612A8155AB74D005750A4 /* AppDelegate.h */,
124127
537612A9155AB74D005750A4 /* AppDelegate.m */,
125128
537612AB155AB74D005750A4 /* MasterViewController.h */,
@@ -235,6 +238,7 @@
235238
53A2B50D155B155A00B12423 /* placeholder.png in Resources */,
236239
53A2B50E155B155A00B12423 /* [email protected] in Resources */,
237240
53EEC18916484553007601E1 /* [email protected] in Resources */,
241+
3E75A9861742DBE700DA412D /* CustomPathImages in Resources */,
238242
);
239243
runOnlyForDeploymentPostprocessing = 0;
240244
};

Examples/SDWebImage Demo/AppDelegate.m

+6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010

1111
#import "MasterViewController.h"
1212

13+
#import <SDWebImage/SDImageCache.h>
14+
1315
@implementation AppDelegate
1416

1517
@synthesize window = _window;
1618
@synthesize navigationController = _navigationController;
1719

1820
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
1921
{
22+
//Add a custom read-only cache path
23+
NSString *bundledPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CustomPathImages"];
24+
[[SDImageCache sharedImageCache] addReadOnlyCachePath:bundledPath];
25+
2026
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
2127
// Override point for customization after application launch.
2228

SDWebImage/SDImageCache.h

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ typedef enum SDImageCacheType SDImageCacheType;
5656
*/
5757
- (id)initWithNamespace:(NSString *)ns;
5858

59+
/**
60+
* Add a read-only cache path to search for images pre-cached by SDImageCache
61+
* Useful if you want to bundle pre-loaded images with your app
62+
*
63+
* @param path The path to use for this read-only cache path
64+
*/
65+
- (void)addReadOnlyCachePath:(NSString *)path;
66+
5967
/**
6068
* Store an image into memory and disk cache at the given key.
6169
*

SDWebImage/SDImageCache.m

+51-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ @interface SDImageCache ()
1919

2020
@property (strong, nonatomic) NSCache *memCache;
2121
@property (strong, nonatomic) NSString *diskCachePath;
22+
@property (strong, nonatomic) NSMutableArray *customPaths;
2223
@property (SDDispatchQueueSetterSementics, nonatomic) dispatch_queue_t ioQueue;
2324

2425
@end
@@ -82,17 +83,41 @@ - (void)dealloc
8283
SDDispatchQueueRelease(_ioQueue);
8384
}
8485

86+
- (void)addReadOnlyCachePath:(NSString *)path
87+
{
88+
if (!self.customPaths)
89+
{
90+
self.customPaths = NSMutableArray.new;
91+
}
92+
93+
if (![self.customPaths containsObject:path])
94+
{
95+
[self.customPaths addObject:path];
96+
}
97+
}
98+
8599
#pragma mark SDImageCache (private)
86100

87-
- (NSString *)cachePathForKey:(NSString *)key
101+
- (NSString *)cachePathForKey:(NSString *)key inPath:(NSString *)path
102+
{
103+
NSString *filename = [self cachedFileNameForKey:key];
104+
return [path stringByAppendingPathComponent:filename];
105+
}
106+
107+
- (NSString *)defaultCachePathForKey:(NSString *)key
108+
{
109+
return [self cachePathForKey:key inPath:self.diskCachePath];
110+
}
111+
112+
- (NSString *)cachedFileNameForKey:(NSString *)key
88113
{
89114
const char *str = [key UTF8String];
90115
unsigned char r[CC_MD5_DIGEST_LENGTH];
91116
CC_MD5(str, (CC_LONG)strlen(str), r);
92117
NSString *filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
93118
r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]];
94119

95-
return [self.diskCachePath stringByAppendingPathComponent:filename];
120+
return filename;
96121
}
97122

98123
#pragma mark ImageCache
@@ -134,7 +159,7 @@ - (void)storeImage:(UIImage *)image imageData:(NSData *)imageData forKey:(NSStri
134159
[fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
135160
}
136161

137-
[fileManager createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil];
162+
[fileManager createFileAtPath:[self defaultCachePathForKey:key] contents:data attributes:nil];
138163
}
139164
});
140165
}
@@ -175,10 +200,30 @@ - (UIImage *)imageFromDiskCacheForKey:(NSString *)key
175200
return diskImage;
176201
}
177202

203+
- (NSData *)diskImageDataBySearchingAllPathsForKey:(NSString *)key
204+
{
205+
NSString *defaultPath = [self defaultCachePathForKey:key];
206+
NSData *data = [NSData dataWithContentsOfFile:defaultPath];
207+
if (data)
208+
{
209+
return data;
210+
}
211+
212+
for (NSString *path in self.customPaths)
213+
{
214+
NSString *filePath = [self cachePathForKey:key inPath:path];
215+
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
216+
if (imageData) {
217+
return imageData;
218+
}
219+
}
220+
221+
return nil;
222+
}
223+
178224
- (UIImage *)diskImageForKey:(NSString *)key
179225
{
180-
NSString *path = [self cachePathForKey:key];
181-
NSData *data = [NSData dataWithContentsOfFile:path];
226+
NSData *data = [self diskImageDataBySearchingAllPathsForKey:key];
182227
if (data)
183228
{
184229
if ([data sd_isGIF])
@@ -259,7 +304,7 @@ - (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk
259304
{
260305
dispatch_async(self.ioQueue, ^
261306
{
262-
[[NSFileManager defaultManager] removeItemAtPath:[self cachePathForKey:key] error:nil];
307+
[[NSFileManager defaultManager] removeItemAtPath:[self defaultCachePathForKey:key] error:nil];
263308
});
264309
}
265310
}

0 commit comments

Comments
 (0)