9
9
#import " YYCache+SDAdditions.h"
10
10
#import " YYMemoryCache+SDAdditions.h"
11
11
#import " YYDiskCache+SDAdditions.h"
12
+ #import " SDImageTransformer.h" // TODO, remove this
13
+ #import < objc/runtime.h>
12
14
13
- static void SDYYPluginUnarchiveObject (NSData *data, UIImage *image) {
14
- if (!data || !image) {
15
+ // TODO, remove this
16
+ static BOOL SDIsThumbnailKey (NSString *key) {
17
+ if ([key rangeOfString: @" -Thumbnail(" ].location != NSNotFound ) {
18
+ return YES ;
19
+ }
20
+ return NO ;
21
+ }
22
+
23
+ @implementation YYCache (SDAdditions)
24
+
25
+ - (SDImageCacheConfig *)config {
26
+ // Provided the default one
27
+ SDImageCacheConfig *config = objc_getAssociatedObject (self, @selector (config ));
28
+ if (!config) {
29
+ config = SDImageCacheConfig.defaultCacheConfig ;
30
+ config.shouldDisableiCloud
31
+ [self setConfig: config];
32
+ }
33
+ return config;
34
+ }
35
+
36
+ - (void )setConfig : (SDImageCacheConfig *)config {
37
+ objc_setAssociatedObject (self, @selector (config ), config, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
38
+ }
39
+
40
+ - (void )_syncDiskToMemoryWithImage : (UIImage *)diskImage forKey : (NSString *)key {
41
+ // earily check
42
+ if (!self.config .shouldCacheImagesInMemory ) {
43
+ return ;
44
+ }
45
+ if (!diskImage) {
46
+ return ;
47
+ }
48
+ // The disk -> memory sync logic, which should only store thumbnail image with thumbnail key
49
+ // However, caller (like SDWebImageManager) will query full key, with thumbnail size, and get thubmnail image
50
+ // We should add a check here, currently it's a hack
51
+ if (diskImage.sd_isThumbnail && !SDIsThumbnailKey (key)) {
52
+ SDImageCoderOptions *options = diskImage.sd_decodeOptions ;
53
+ CGSize thumbnailSize = CGSizeZero ;
54
+ NSValue *thumbnailSizeValue = options[SDImageCoderDecodeThumbnailPixelSize];
55
+ if (thumbnailSizeValue != nil ) {
56
+ #if SD_MAC
57
+ thumbnailSize = thumbnailSizeValue.sizeValue ;
58
+ #else
59
+ thumbnailSize = thumbnailSizeValue.CGSizeValue ;
60
+ #endif
61
+ }
62
+ BOOL preserveAspectRatio = YES ;
63
+ NSNumber *preserveAspectRatioValue = options[SDImageCoderDecodePreserveAspectRatio];
64
+ if (preserveAspectRatioValue != nil ) {
65
+ preserveAspectRatio = preserveAspectRatioValue.boolValue ;
66
+ }
67
+ // Calculate the actual thumbnail key
68
+ NSString *thumbnailKey = SDThumbnailedKeyForKey (key, thumbnailSize, preserveAspectRatio);
69
+ // Override the sync key
70
+ key = thumbnailKey;
71
+ }
72
+ NSUInteger cost = diskImage.sd_memoryCost ;
73
+ [self .memoryCache setObject: diskImage forKey: key cost: cost];
74
+ }
75
+
76
+ - (void )_unarchiveObjectWithImage : (UIImage *)image forKey : (NSString *)key {
77
+ if (!image || !key) {
15
78
return ;
16
79
}
17
80
// Check extended data
18
- NSData *extendedData = [YYDiskCache getExtendedDataFromObject: data ];
81
+ NSData *extendedData = [self .diskCache extendedDataForKey: key ];
19
82
if (!extendedData) {
20
83
return ;
21
84
}
@@ -41,8 +104,8 @@ static void SDYYPluginUnarchiveObject(NSData *data, UIImage *image) {
41
104
image.sd_extendedObject = extendedObject;
42
105
}
43
106
44
- static void SDYYPluginArchiveObject ( NSData *data, UIImage *image) {
45
- if (!data || !image ) {
107
+ - ( void ) _archivedDataWithImage : (UIImage *) image forKey : ( NSString *) key {
108
+ if (!image || !key ) {
46
109
return ;
47
110
}
48
111
// Check extended data
@@ -68,12 +131,10 @@ static void SDYYPluginArchiveObject(NSData *data, UIImage *image) {
68
131
}
69
132
}
70
133
if (extendedData) {
71
- [YYDiskCache setExtendedData: extendedData toObject: data ];
134
+ [self .diskCache setExtendedData: extendedData forKey: key ];
72
135
}
73
136
}
74
137
75
- @implementation YYCache (SDAdditions)
76
-
77
138
- (id <SDWebImageOperation>)queryImageForKey : (NSString *)key options : (SDWebImageOptions)options context : (SDWebImageContext *)context completion : (SDImageCacheQueryCompletionBlock)doneBlock {
78
139
return [self queryImageForKey: key options: options context: context cacheType: SDImageCacheTypeAll completion: doneBlock];
79
140
}
@@ -102,7 +163,7 @@ @implementation YYCache (SDAdditions)
102
163
if (image) {
103
164
if (options & SDWebImageDecodeFirstFrameOnly) {
104
165
// Ensure static image
105
- if (image.sd_isAnimated ) {
166
+ if (image.sd_imageFrameCount > 1 ) {
106
167
#if SD_MAC
107
168
image = [[NSImage alloc ] initWithCGImage: image.CGImage scale: image.scale orientation: kCGImagePropertyOrientationUp ];
108
169
#else
@@ -156,30 +217,23 @@ @implementation YYCache (SDAdditions)
156
217
// the image is from in-memory cache, but need image data
157
218
diskImage = image;
158
219
} else if (diskData) {
159
- BOOL shouldCacheToMomery = YES ;
220
+ BOOL shouldCacheToMemory = YES ;
160
221
if (context[SDWebImageContextStoreCacheType]) {
161
222
SDImageCacheType cacheType = [context[SDWebImageContextStoreCacheType] integerValue ];
162
- shouldCacheToMomery = (cacheType == SDImageCacheTypeAll || cacheType == SDImageCacheTypeMemory);
223
+ shouldCacheToMemory = (cacheType == SDImageCacheTypeAll || cacheType == SDImageCacheTypeMemory);
163
224
}
164
- CGSize thumbnailSize = CGSizeZero ;
165
- NSValue *thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize];
166
- if (thumbnailSizeValue != nil ) {
167
- #if SD_MAC
168
- thumbnailSize = thumbnailSizeValue.sizeValue ;
169
- #else
170
- thumbnailSize = thumbnailSizeValue.CGSizeValue ;
171
- #endif
172
- }
173
- if (thumbnailSize.width > 0 && thumbnailSize.height > 0 ) {
174
- // Query full size cache key which generate a thumbnail, should not write back to full size memory cache
175
- shouldCacheToMomery = NO ;
225
+ // Special case: If user query image in list for the same URL, to avoid decode and write **same** image object into disk cache multiple times, we query and check memory cache here again.
226
+ if (shouldCacheToMemory && self.config .shouldCacheImagesInMemory ) {
227
+ diskImage = [self .memoryCache objectForKey: key];
176
228
}
177
229
// decode image data only if in-memory cache missed
178
- diskImage = SDImageCacheDecodeImageData (diskData, key, options, context);
179
- SDYYPluginUnarchiveObject (diskData, diskImage);
180
- if (shouldCacheToMomery && diskImage) {
181
- NSUInteger cost = diskImage.sd_memoryCost ;
182
- [self .memoryCache setObject: diskImage forKey: key cost: cost];
230
+ if (!diskImage) {
231
+ diskImage = SDImageCacheDecodeImageData (diskData, key, options, context);
232
+ [self _unarchiveObjectWithImage: diskImage forKey: key];
233
+ // check if we need sync logic
234
+ if (shouldCacheToMemory) {
235
+ [self _syncDiskToMemoryWithImage: diskImage forKey: key];
236
+ }
183
237
}
184
238
}
185
239
return diskImage;
@@ -264,8 +318,8 @@ - (void)storeImage:(UIImage *)image imageData:(NSData *)imageData forKey:(NSStri
264
318
}
265
319
}
266
320
NSData *data = [[SDImageCodersManager sharedManager ] encodedDataWithImage: image format: format options: context[SDWebImageContextImageEncodeOptions]];
267
- SDYYPluginArchiveObject (data, image);
268
321
[self .diskCache setObject: data forKey: key withBlock: ^{
322
+ [self _archivedDataWithImage: image forKey: key];
269
323
if (completionBlock) {
270
324
[(queue ?: SDCallbackQueue.mainQueue) async: ^{
271
325
completionBlock ();
@@ -274,8 +328,8 @@ - (void)storeImage:(UIImage *)image imageData:(NSData *)imageData forKey:(NSStri
274
328
}];
275
329
});
276
330
} else {
277
- SDYYPluginArchiveObject (data, image);
278
331
[self .diskCache setObject: data forKey: key withBlock: ^{
332
+ [self _archivedDataWithImage: image forKey: key];
279
333
if (completionBlock) {
280
334
[(queue ?: SDCallbackQueue.mainQueue) async: ^{
281
335
completionBlock ();
0 commit comments