Skip to content

Commit 5993bb4

Browse files
committed
Sync the disk cache store logic from SDWebImage 5.6.0
1 parent 744771c commit 5993bb4

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

SDWebImageYYPlugin/Classes/YYCache/YYCacheBridge/YYCache+SDAdditions.m

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,25 @@
1212

1313
static NSData * SDYYPluginCacheDataWithImageData(UIImage *image, NSData *imageData) {
1414
NSData *data = imageData;
15+
if (!data && [image conformsToProtocol:@protocol(SDAnimatedImage)]) {
16+
// If image is custom animated image class, prefer its original animated data
17+
data = [((id<SDAnimatedImage>)image) animatedImageData];
18+
}
1519
if (!data && image) {
16-
// If we do not have any data to detect image format, check whether it contains alpha channel to use PNG or JPEG format
17-
SDImageFormat format;
18-
if ([SDImageCoderHelper CGImageContainsAlpha:image.CGImage]) {
19-
format = SDImageFormatPNG;
20-
} else {
21-
format = SDImageFormatJPEG;
20+
// Check image's associated image format, may return .undefined
21+
SDImageFormat format = image.sd_imageFormat;
22+
if (format == SDImageFormatUndefined) {
23+
// If image is animated, use GIF (APNG may be better, but has bugs before macOS 10.14)
24+
if (image.sd_isAnimated) {
25+
format = SDImageFormatGIF;
26+
} else {
27+
// If we do not have any data to detect image format, check whether it contains alpha channel to use PNG or JPEG format
28+
if ([SDImageCoderHelper CGImageContainsAlpha:image.CGImage]) {
29+
format = SDImageFormatPNG;
30+
} else {
31+
format = SDImageFormatJPEG;
32+
}
33+
}
2234
}
2335
data = [[SDImageCodersManager sharedManager] encodedDataWithImage:image format:format options:nil];
2436
}
@@ -116,7 +128,10 @@ @implementation YYCache (SDAdditions)
116128
}
117129
} else {
118130
@try {
131+
#pragma clang diagnostic push
132+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
119133
extendedObject = [NSKeyedUnarchiver unarchiveObjectWithData:extendedData];
134+
#pragma clang diagnostic pop
120135
} @catch (NSException *exception) {
121136
NSLog(@"NSKeyedUnarchiver unarchive failed with exception: %@", exception);
122137
}
@@ -183,7 +198,10 @@ - (void)storeImageToDisk:(UIImage *)image imageData:(NSData *)imageData forKey:(
183198
}
184199
} else {
185200
@try {
201+
#pragma clang diagnostic push
202+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
186203
extendedData = [NSKeyedArchiver archivedDataWithRootObject:extendedObject];
204+
#pragma clang diagnostic pop
187205
} @catch (NSException *exception) {
188206
NSLog(@"NSKeyedArchiver archive failed with exception: %@", exception);
189207
}

0 commit comments

Comments
 (0)