Update SDImageCache.m
This commit is contained in:
parent
88b3ba211c
commit
5c88cf19ed
|
@ -194,7 +194,12 @@ static NSString * _defaultDiskCacheDirectory;
|
|||
[self.memoryCache setObject:image forKey:key cost:cost];
|
||||
}
|
||||
|
||||
if (toDisk) {
|
||||
if (!toDisk) {
|
||||
if (completionBlock) {
|
||||
completionBlock();
|
||||
}
|
||||
return;
|
||||
}
|
||||
dispatch_async(self.ioQueue, ^{
|
||||
@autoreleasepool {
|
||||
NSData *data = imageData;
|
||||
|
@ -211,20 +216,32 @@ static NSString * _defaultDiskCacheDirectory;
|
|||
format = SDImageFormatGIF;
|
||||
} else {
|
||||
// If we do not have any data to detect image format, check whether it contains alpha channel to use PNG or JPEG format
|
||||
if ([SDImageCoderHelper CGImageContainsAlpha:image.CGImage]) {
|
||||
format = SDImageFormatPNG;
|
||||
} else {
|
||||
format = SDImageFormatJPEG;
|
||||
}
|
||||
format = [SDImageCoderHelper CGImageContainsAlpha:image.CGImage] ? SDImageFormatPNG : SDImageFormatJPEG;
|
||||
}
|
||||
}
|
||||
data = [[SDImageCodersManager sharedManager] encodedDataWithImage:image format:format options:nil];
|
||||
}
|
||||
[self _storeImageDataToDisk:data forKey:key];
|
||||
if (image) {
|
||||
[self _archivedDataWithImage:image forKey:key];
|
||||
}
|
||||
|
||||
if (completionBlock) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
completionBlock();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
- (void)_archivedDataWithImage:(UIImage *)image forKey:(NSString *)key {
|
||||
if (!image) {
|
||||
return;
|
||||
}
|
||||
// Check extended data
|
||||
id extendedObject = image.sd_extendedObject;
|
||||
if ([extendedObject conformsToProtocol:@protocol(NSCoding)]) {
|
||||
if (![extendedObject conformsToProtocol:@protocol(NSCoding)]) {
|
||||
return;
|
||||
}
|
||||
NSData *extendedData;
|
||||
if (@available(iOS 11, tvOS 11, macOS 10.13, watchOS 4, *)) {
|
||||
NSError *error;
|
||||
|
@ -246,21 +263,6 @@ static NSString * _defaultDiskCacheDirectory;
|
|||
[self.diskCache setExtendedData:extendedData forKey:key];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (completionBlock) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
completionBlock();
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (completionBlock) {
|
||||
completionBlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)storeImageToMemory:(UIImage *)image forKey:(NSString *)key {
|
||||
if (!image || !key) {
|
||||
|
@ -414,12 +416,23 @@ static NSString * _defaultDiskCacheDirectory;
|
|||
}
|
||||
|
||||
- (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NSData *)data options:(SDImageCacheOptions)options context:(SDWebImageContext *)context {
|
||||
if (data) {
|
||||
if (!data) {
|
||||
return nil;
|
||||
}
|
||||
UIImage *image = SDImageCacheDecodeImageData(data, key, [[self class] imageOptionsFromCacheOptions:options], context);
|
||||
if (image) {
|
||||
[self _unarchiveObjectWithImage:image forKey:key];
|
||||
return image;
|
||||
}
|
||||
|
||||
- (void)_unarchiveObjectWithImage:(UIImage *)image forKey:(NSString *)key {
|
||||
if (!image) {
|
||||
return;
|
||||
}
|
||||
// Check extended data
|
||||
NSData *extendedData = [self.diskCache extendedDataForKey:key];
|
||||
if (extendedData) {
|
||||
if (!extendedData) {
|
||||
return;
|
||||
}
|
||||
id extendedObject;
|
||||
if (@available(iOS 11, tvOS 11, macOS 10.13, watchOS 4, *)) {
|
||||
NSError *error;
|
||||
|
@ -441,12 +454,6 @@ static NSString * _defaultDiskCacheDirectory;
|
|||
}
|
||||
image.sd_extendedObject = extendedObject;
|
||||
}
|
||||
}
|
||||
return image;
|
||||
} else {
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (nullable NSOperation *)queryCacheOperationForKey:(NSString *)key done:(SDImageCacheQueryCompletionBlock)doneBlock {
|
||||
return [self queryCacheOperationForKey:key options:0 done:doneBlock];
|
||||
|
|
Loading…
Reference in New Issue