@@ -19,6 +19,7 @@ @interface SDImageCache ()
19
19
20
20
@property (strong , nonatomic ) NSCache *memCache;
21
21
@property (strong , nonatomic ) NSString *diskCachePath;
22
+ @property (strong , nonatomic ) NSMutableArray *customPaths;
22
23
@property (SDDispatchQueueSetterSementics, nonatomic ) dispatch_queue_t ioQueue;
23
24
24
25
@end
@@ -82,17 +83,41 @@ - (void)dealloc
82
83
SDDispatchQueueRelease (_ioQueue);
83
84
}
84
85
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
+
85
99
#pragma mark SDImageCache (private)
86
100
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
88
113
{
89
114
const char *str = [key UTF8String ];
90
115
unsigned char r[CC_MD5_DIGEST_LENGTH];
91
116
CC_MD5 (str, (CC_LONG)strlen (str), r);
92
117
NSString *filename = [NSString stringWithFormat: @" %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x " ,
93
118
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 ]];
94
119
95
- return [ self .diskCachePath stringByAppendingPathComponent: filename] ;
120
+ return filename;
96
121
}
97
122
98
123
#pragma mark ImageCache
@@ -134,7 +159,7 @@ - (void)storeImage:(UIImage *)image imageData:(NSData *)imageData forKey:(NSStri
134
159
[fileManager createDirectoryAtPath: _diskCachePath withIntermediateDirectories: YES attributes: nil error: NULL ];
135
160
}
136
161
137
- [fileManager createFileAtPath: [self cachePathForKey : key] contents: data attributes: nil ];
162
+ [fileManager createFileAtPath: [self defaultCachePathForKey : key] contents: data attributes: nil ];
138
163
}
139
164
});
140
165
}
@@ -175,10 +200,30 @@ - (UIImage *)imageFromDiskCacheForKey:(NSString *)key
175
200
return diskImage;
176
201
}
177
202
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
+
178
224
- (UIImage *)diskImageForKey : (NSString *)key
179
225
{
180
- NSString *path = [self cachePathForKey: key];
181
- NSData *data = [NSData dataWithContentsOfFile: path];
226
+ NSData *data = [self diskImageDataBySearchingAllPathsForKey: key];
182
227
if (data)
183
228
{
184
229
if ([data sd_isGIF ])
@@ -259,7 +304,7 @@ - (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk
259
304
{
260
305
dispatch_async (self.ioQueue , ^
261
306
{
262
- [[NSFileManager defaultManager ] removeItemAtPath: [self cachePathForKey : key] error: nil ];
307
+ [[NSFileManager defaultManager ] removeItemAtPath: [self defaultCachePathForKey : key] error: nil ];
263
308
});
264
309
}
265
310
}
0 commit comments