Make some simple logical adjustments
This commit is contained in:
parent
a84ade991c
commit
ee18f7f55d
|
@ -42,24 +42,6 @@ typedef NSString * _Nullable (^SDImageCacheAdditionalCachePathBlock)(NSString *
|
|||
typedef void(^SDImageCacheQueryCompletionBlock)(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType);
|
||||
typedef void(^SDImageCacheContainsCompletionBlock)(SDImageCacheType containsCacheType);
|
||||
|
||||
#pragma mark - Context Options
|
||||
|
||||
/**
|
||||
A `UIImage` instance from `SDWebImageCache` when miss normal cache and you specify `SDWebImageContextOriginalQueryCacheType`
|
||||
and image cache hit. This can be used directly instead of downloading.
|
||||
@note If you don't implement `SDWebImageContextOriginalQueryCacheType` support, you do not need to care abot this context option.
|
||||
*/
|
||||
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextOriginalCachedImage;
|
||||
|
||||
/**
|
||||
A `NSData` instance from `SDWebImageCache` when miss normal cache and you specify `SDWebImageContextOriginalQueryCacheType`
|
||||
and image cache hit. This can be used directly instead of downloading.
|
||||
@note If you don't implement `SDWebImageContextOriginalQueryCacheType` support, you do not need to care abot this context option.
|
||||
*/
|
||||
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextOriginalCachedImageData;
|
||||
|
||||
#pragma mark - Helper method
|
||||
|
||||
/**
|
||||
This is the built-in decoding process for image query from cache.
|
||||
@note If you want to implement your custom loader with `queryImageForKey:options:context:completion:` API, but also want to keep compatible with SDWebImage's behavior, you'd better use this to produce image.
|
||||
|
@ -72,8 +54,6 @@ FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextOrigin
|
|||
*/
|
||||
FOUNDATION_EXPORT UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSString * _Nonnull cacheKey, SDWebImageOptions options, SDWebImageContext * _Nullable context);
|
||||
|
||||
#pragma mark - SDImageCache
|
||||
|
||||
/**
|
||||
This is the image cache protocol to provide custom image cache for `SDWebImageManager`.
|
||||
Though the best practice to custom image cache, is to write your own class which conform `SDMemoryCache` or `SDDiskCache` protocol for `SDImageCache` class (See more on `SDImageCacheConfig.memoryCacheClass & SDImageCacheConfig.diskCacheClass`).
|
||||
|
|
|
@ -83,8 +83,3 @@ UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSS
|
|||
|
||||
return image;
|
||||
}
|
||||
|
||||
#pragma mark - Context option
|
||||
|
||||
SDWebImageContextOption const SDWebImageContextOriginalCachedImage = @"originalCachedImage";
|
||||
SDWebImageContextOption const SDWebImageContextOriginalCachedImageData = @"originalCachedImageData";
|
||||
|
|
|
@ -262,8 +262,8 @@ static id<SDImageLoader> _defaultImageLoader;
|
|||
[self safelyRemoveOperationFromRunning:operation];
|
||||
return;
|
||||
}
|
||||
else if (!cachedImage) {
|
||||
// Miss normal cache, we have a chance to quary original image from cache instead of downloading
|
||||
else if (context[SDWebImageContextImageTransformer] && !cachedImage) {
|
||||
// Have a chance to quary original cache instead of downloading
|
||||
[self callOriginalCacheProcessForOperation:operation url:url options:options context:context progress:progressBlock completed:completedBlock];
|
||||
return ;
|
||||
}
|
||||
|
@ -299,7 +299,7 @@ static id<SDImageLoader> _defaultImageLoader;
|
|||
}
|
||||
|
||||
// Check whether we should query original cache
|
||||
BOOL shouldQueryOriginalCache = context[SDWebImageContextImageTransformer] && (originalQueryCacheType != SDImageCacheTypeNone);
|
||||
BOOL shouldQueryOriginalCache = (originalQueryCacheType != SDImageCacheTypeNone);
|
||||
if (shouldQueryOriginalCache) {
|
||||
// Change originContext to mutable
|
||||
SDWebImageMutableContext * __block originContext;
|
||||
|
@ -309,7 +309,7 @@ static id<SDImageLoader> _defaultImageLoader;
|
|||
originContext = [NSMutableDictionary dictionary];
|
||||
}
|
||||
|
||||
// disable transformer for cache key generation
|
||||
// Disable transformer for cache key generation
|
||||
id<SDImageTransformer> transformer = originContext[SDWebImageContextImageTransformer];
|
||||
originContext[SDWebImageContextImageTransformer] = [NSNull null];
|
||||
|
||||
|
@ -329,17 +329,10 @@ static id<SDImageLoader> _defaultImageLoader;
|
|||
originContext[SDWebImageContextImageTransformer] = transformer;
|
||||
}
|
||||
|
||||
// Pass the original cached image and date to the next process
|
||||
if (cachedImage) {
|
||||
originContext[SDWebImageContextOriginalCachedImage] = cachedImage;
|
||||
}
|
||||
// Use the store cache process instead of downloading, and ignore .refreshCached option for now
|
||||
[self callStoreCacheProcessForOperation:operation url:url options:options context:context downloadedImage:cachedImage downloadedData:cachedData finished:YES progress:progressBlock completed:completedBlock];
|
||||
|
||||
if (cachedData) {
|
||||
originContext[SDWebImageContextOriginalCachedImageData] = cachedData;
|
||||
}
|
||||
|
||||
// Continue download process
|
||||
[self callDownloadProcessForOperation:operation url:url options:options context:[originContext copy] cachedImage:nil cachedData:nil cacheType:originalQueryCacheType progress:progressBlock completed:completedBlock];
|
||||
[self safelyRemoveOperationFromRunning:operation];
|
||||
}];
|
||||
}
|
||||
else {
|
||||
|
@ -366,12 +359,9 @@ static id<SDImageLoader> _defaultImageLoader;
|
|||
imageLoader = self.imageLoader;
|
||||
}
|
||||
|
||||
// Get original chched image
|
||||
UIImage *originalCachedImage = context[SDWebImageContextOriginalCachedImage];
|
||||
|
||||
// Check whether we should download image from network
|
||||
BOOL shouldDownload = !SD_OPTIONS_CONTAINS(options, SDWebImageFromCacheOnly);
|
||||
shouldDownload &= ((!cachedImage && !originalCachedImage) || options & SDWebImageRefreshCached);
|
||||
shouldDownload &= (!cachedImage || options & SDWebImageRefreshCached);
|
||||
shouldDownload &= (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url]);
|
||||
shouldDownload &= [imageLoader canRequestImageForURL:url];
|
||||
if (shouldDownload) {
|
||||
|
@ -427,10 +417,6 @@ static id<SDImageLoader> _defaultImageLoader;
|
|||
} else if (cachedImage) {
|
||||
[self callCompletionBlockForOperation:operation completion:completedBlock image:cachedImage data:cachedData error:nil cacheType:cacheType finished:YES url:url];
|
||||
[self safelyRemoveOperationFromRunning:operation];
|
||||
} else if (originalCachedImage) {
|
||||
// Still use the store cache process, but only instead of downloading
|
||||
[self callStoreCacheProcessForOperation:operation url:url options:options context:context downloadedImage:originalCachedImage downloadedData:context[SDWebImageContextOriginalCachedImageData] finished:YES progress:progressBlock completed:completedBlock];
|
||||
[self safelyRemoveOperationFromRunning:operation];
|
||||
} else {
|
||||
// Image not in cache and download disallowed by delegate
|
||||
[self callCompletionBlockForOperation:operation completion:completedBlock image:nil data:nil error:nil cacheType:SDImageCacheTypeNone finished:YES url:url];
|
||||
|
|
Loading…
Reference in New Issue