Merge pull request #2186 from dreampiggy/feature_image_from_cache_only

Add a option SDWebImageFromCacheOnly to load the image from cache only and prevent network
This commit is contained in:
DreamPiggy 2018-01-23 11:51:09 +08:00 committed by GitHub
commit 0faf0db5b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View File

@ -98,15 +98,20 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
/** /**
* By default, we do not query disk data when the image is cached in memory. This mask can force to query disk data at the same time. * By default, we do not query disk data when the image is cached in memory. This mask can force to query disk data at the same time.
* This options is recommend to be used with `SDWebImageQueryDiskSync` to ensure the image is loaded in the same runloop. * This flag is recommend to be used with `SDWebImageQueryDiskSync` to ensure the image is loaded in the same runloop.
*/ */
SDWebImageQueryDataWhenInMemory = 1 << 13, SDWebImageQueryDataWhenInMemory = 1 << 13,
/** /**
* By default, we query the memory cache synchronously, disk cache asynchronously. This mask can force to query disk cache synchronously to ensure that image is loaded in the same runloop. * By default, we query the memory cache synchronously, disk cache asynchronously. This mask can force to query disk cache synchronously to ensure that image is loaded in the same runloop.
* This can avoid flashing during cell reuse if you disable memory cache or in some other cases. * This flag can avoid flashing during cell reuse if you disable memory cache or in some other cases.
*/ */
SDWebImageQueryDiskSync = 1 << 14 SDWebImageQueryDiskSync = 1 << 14,
/**
* By default, when the cache missed, the image is download from the network. This flag can prevent network to load from cache only.
*/
SDWebImageFromCacheOnly = 1 << 15
}; };
typedef void(^SDExternalCompletionBlock)(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL); typedef void(^SDExternalCompletionBlock)(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL);

View File

@ -158,7 +158,11 @@
return; return;
} }
if ((!cachedImage || options & SDWebImageRefreshCached) && (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url])) { // Check whether we should download image from network
BOOL shouldDownload = (!(options & SDWebImageFromCacheOnly))
&& (!cachedImage || options & SDWebImageRefreshCached)
&& (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url]);
if (shouldDownload) {
if (cachedImage && options & SDWebImageRefreshCached) { if (cachedImage && options & SDWebImageRefreshCached) {
// If image was found in the cache but SDWebImageRefreshCached is provided, notify about the cached image // If image was found in the cache but SDWebImageRefreshCached is provided, notify about the cached image
// AND try to re-download it in order to let a chance to NSURLCache to refresh it from server. // AND try to re-download it in order to let a chance to NSURLCache to refresh it from server.