From c95e139c7b5b453f734e89803580b2922c082fd5 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sun, 21 Jan 2018 13:26:30 +0800 Subject: [PATCH] Add a option SDWebImageFromCacheOnly to load the image from cache only and prevent network --- SDWebImage/SDWebImageManager.h | 11 ++++++++--- SDWebImage/SDWebImageManager.m | 8 ++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/SDWebImage/SDWebImageManager.h b/SDWebImage/SDWebImageManager.h index 02e313ca..dbe84fa9 100644 --- a/SDWebImage/SDWebImageManager.h +++ b/SDWebImage/SDWebImageManager.h @@ -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. - * 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, /** * 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); diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index e4422d5d..b15e5e7c 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -157,8 +157,12 @@ [self safelyRemoveOperationFromRunning:strongOperation]; 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 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.