13
13
#import < mach/mach.h>
14
14
#import < mach/mach_host.h>
15
15
16
+ // <NTES DIY SDWebImage>
17
+ #ifndef kAppRootDirectory
18
+ #define kAppRootDirectory @" UserData" // set up for application data root dir
19
+ #endif
20
+ #define NEW_ETERNAL_CACHE_DIR_NAME @" ImageStorage"
21
+
16
22
static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7 ; // 1 week
17
23
// PNG signature bytes and data (below)
18
24
static unsigned char kPNGSignatureBytes [8 ] = {0x89 , 0x50 , 0x4E , 0x47 , 0x0D , 0x0A , 0x1A , 0x0A };
@@ -40,6 +46,9 @@ @interface SDImageCache ()
40
46
@property (strong , nonatomic ) NSMutableArray *customPaths;
41
47
@property (SDDispatchQueueSetterSementics, nonatomic ) dispatch_queue_t ioQueue;
42
48
49
+ // <NTES DIY SDWebImage>
50
+ @property (strong , nonatomic ) NSString *eternalDiskCachePath;
51
+
43
52
@end
44
53
45
54
@@ -82,9 +91,15 @@ - (id)initWithNamespace:(NSString *)ns
82
91
_memCache.name = fullNamespace;
83
92
84
93
// Init the disk cache
85
- NSArray *paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
86
- _diskCachePath = [paths[0 ] stringByAppendingPathComponent: fullNamespace];
87
-
94
+ // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
95
+ // _diskCachePath = [paths[0] stringByAppendingPathComponent:fullNamespace];
96
+
97
+ // <NTES DIY SDWebImage> init the disk cache
98
+ NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
99
+ NSString *rootDir = [paths[0 ] stringByAppendingPathComponent: kAppRootDirectory ];
100
+ _diskCachePath = [rootDir stringByAppendingPathComponent: fullNamespace];
101
+ _eternalDiskCachePath = [rootDir stringByAppendingPathComponent: NEW_ETERNAL_CACHE_DIR_NAME];
102
+
88
103
dispatch_sync (_ioQueue, ^
89
104
{
90
105
_fileManager = NSFileManager .new ;
@@ -144,6 +159,12 @@ - (NSString *)defaultCachePathForKey:(NSString *)key
144
159
return [self cachePathForKey: key inPath: self .diskCachePath];
145
160
}
146
161
162
+ // <NTES DIY SDWebImage> 离线图片的文件路径
163
+ - (NSString *)eternalCachePathForKey : (NSString *)key
164
+ {
165
+ return [self cachePathForKey: key inPath: self .eternalDiskCachePath];
166
+ }
167
+
147
168
- (NSString *)cachedFileNameForKey : (NSString *)key
148
169
{
149
170
const char *str = [key UTF8String ];
@@ -223,22 +244,108 @@ - (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate image
223
244
}
224
245
}
225
246
247
+ /* *
248
+ 不改原始方法-->
249
+ - (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk
250
+
251
+ */
252
+ // <NTES DIY SDWebImage>(新增方法)
253
+ - (void )storeImage : (UIImage *)image recalculateFromImage : (BOOL )recalculate imageData : (NSData *)imageData forKey : (NSString *)key toDisk : (BOOL )toDisk isEternal : (BOOL )isEternal
254
+ {
255
+ if (!isEternal)
256
+ {
257
+ [self storeImage: image recalculateFromImage: recalculate imageData: imageData forKey: key toDisk: toDisk];
258
+ return ;
259
+ }
260
+
261
+ if (!image || !key)
262
+ {
263
+ return ;
264
+ }
265
+
266
+ [self .memCache setObject: image forKey: key cost: image.size.height * image.size.width * image.scale];
267
+
268
+ if (toDisk)
269
+ {
270
+ dispatch_async (self.ioQueue , ^
271
+ {
272
+ NSData *data = imageData;
273
+
274
+ if (image && (recalculate || !data))
275
+ {
276
+ #if TARGET_OS_IPHONE
277
+ // We need to determine if the image is a PNG or a JPEG
278
+ // PNGs are easier to detect because they have a unique signature (http://www.w3.org/TR/PNG-Structure.html)
279
+ // The first eight bytes of a PNG file always contain the following (decimal) values:
280
+ // 137 80 78 71 13 10 26 10
281
+
282
+ // We assume the image is PNG, in case the imageData is nil (i.e. if trying to save a UIImage directly),
283
+ // we will consider it PNG to avoid loosing the transparency
284
+ BOOL imageIsPng = YES ;
285
+
286
+ // But if we have an image data, we will look at the preffix
287
+ if ([imageData length ] >= [kPNGSignatureData length ])
288
+ {
289
+ imageIsPng = ImageDataHasPNGPreffix (imageData);
290
+ }
291
+
292
+ if (imageIsPng)
293
+ {
294
+ data = UIImagePNGRepresentation (image);
295
+ }
296
+ else
297
+ {
298
+ data = UIImageJPEGRepresentation (image, (CGFloat)1.0 );
299
+ }
300
+ #else
301
+ data = [NSBitmapImageRep representationOfImageRepsInArray: image.representations usingType: NSJPEGFileType properties: nil ];
302
+ #endif
303
+ }
304
+
305
+ if (data)
306
+ {
307
+ // Can't use defaultManager another thread
308
+ NSFileManager *fileManager = NSFileManager .new ;
309
+
310
+ if (![fileManager fileExistsAtPath: _eternalDiskCachePath]) // 离线目录
311
+ {
312
+ [fileManager createDirectoryAtPath: _eternalDiskCachePath withIntermediateDirectories: YES attributes: nil error: NULL ];
313
+ }
314
+
315
+ [fileManager createFileAtPath: [self eternalCachePathForKey: key] contents: data attributes: nil ];
316
+ }
317
+ });
318
+ }
319
+ }
320
+
226
321
- (void )storeImage : (UIImage *)image forKey : (NSString *)key
227
322
{
228
323
[self storeImage: image recalculateFromImage: YES imageData: nil forKey: key toDisk: YES ];
229
324
}
230
325
326
+ // <NTES DIY SDWebImage>(新增方法)
327
+ - (void )storeImage : (UIImage *)image forKey : (NSString *)key isEternal : (BOOL )isEternal
328
+ {
329
+ [self storeImage: image recalculateFromImage: YES imageData: nil forKey: key toDisk: YES isEternal: isEternal];
330
+ }
331
+
231
332
- (void )storeImage : (UIImage *)image forKey : (NSString *)key toDisk : (BOOL )toDisk
232
333
{
233
334
[self storeImage: image recalculateFromImage: YES imageData: nil forKey: key toDisk: toDisk];
234
335
}
235
336
337
+ // <NTES DIY SDWebImage>(改动原始方法)
236
338
- (BOOL )diskImageExistsWithKey : (NSString *)key
237
339
{
238
340
__block BOOL exists = NO ;
239
341
dispatch_sync (_ioQueue, ^
240
342
{
241
343
exists = [_fileManager fileExistsAtPath: [self defaultCachePathForKey: key]];
344
+ // <NTES DIY SDWebImage>默认路径找不到,去离线目录找一次
345
+ if (!exists)
346
+ {
347
+ exists = [_fileManager fileExistsAtPath: [self eternalCachePathForKey: key]];
348
+ }
242
349
});
243
350
244
351
return exists;
@@ -269,6 +376,7 @@ - (UIImage *)imageFromDiskCacheForKey:(NSString *)key
269
376
return diskImage;
270
377
}
271
378
379
+ // <NTES DIY SDWebImage>(改动原始方法)
272
380
- (NSData *)diskImageDataBySearchingAllPathsForKey : (NSString *)key
273
381
{
274
382
NSString *defaultPath = [self defaultCachePathForKey: key];
@@ -277,6 +385,14 @@ - (NSData *)diskImageDataBySearchingAllPathsForKey:(NSString *)key
277
385
{
278
386
return data;
279
387
}
388
+
389
+ // <NTES DIY SDWebImage> 查找离线目录
390
+ NSString *eternalPath = [self eternalCachePathForKey: key];
391
+ NSData *eternalData = [NSData dataWithContentsOfFile: eternalPath];
392
+ if (eternalData)
393
+ {
394
+ return eternalData;
395
+ }
280
396
281
397
for (NSString *path in self.customPaths )
282
398
{
@@ -380,6 +496,25 @@ - (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk
380
496
}
381
497
}
382
498
499
+ /* *
500
+ * 从离线目录删除图片
501
+ */
502
+ // <NTES DIY SDWebImage>(新增方法)
503
+ - (void )removeImageFromEternalDisk : (NSString *)key
504
+ {
505
+ if (key == nil )
506
+ {
507
+ return ;
508
+ }
509
+
510
+ [self .memCache removeObjectForKey: key];
511
+
512
+ dispatch_async (self.ioQueue , ^
513
+ {
514
+ [[NSFileManager defaultManager ] removeItemAtPath: [self eternalCachePathForKey: key] error: nil ];
515
+ });
516
+ }
517
+
383
518
- (void )setMaxMemoryCost : (NSUInteger )maxMemoryCost
384
519
{
385
520
self.memCache .totalCostLimit = maxMemoryCost;
0 commit comments