From 6f90bc083af38045bf478a23f8a23ad81061d54e Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 19 Nov 2020 16:17:56 +0800 Subject: [PATCH] Added the options and context arg for Image Loader custom protoocl, this can be used for advanced user to grab and check for context for current loading URL to process the logic. --- SDWebImage/Core/SDImageCache.h | 2 +- SDWebImage/Core/SDImageCoder.h | 2 +- SDWebImage/Core/SDImageIOAnimatedCoder.m | 2 +- SDWebImage/Core/SDImageLoader.h | 36 ++++++++++++++++++++++-- SDWebImage/Core/SDWebImageManager.m | 12 ++++++-- 5 files changed, 47 insertions(+), 7 deletions(-) diff --git a/SDWebImage/Core/SDImageCache.h b/SDWebImage/Core/SDImageCache.h index 7523da41..ad3afd53 100644 --- a/SDWebImage/Core/SDImageCache.h +++ b/SDWebImage/Core/SDImageCache.h @@ -341,7 +341,7 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) { * @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold. * @return The image for the given key, or nil if not found. */ -- (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key options:(SDImageCacheOptions)options context:(nullable SDWebImageContext *)context;; +- (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key options:(SDImageCacheOptions)options context:(nullable SDWebImageContext *)context; #pragma mark - Remove Ops diff --git a/SDWebImage/Core/SDImageCoder.h b/SDWebImage/Core/SDImageCoder.h index f7f63835..53b52e5d 100644 --- a/SDWebImage/Core/SDImageCoder.h +++ b/SDWebImage/Core/SDImageCoder.h @@ -93,7 +93,7 @@ FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeEmbedThumb But this may be useful for some custom coders, because some business logic may dependent on things other than image or image data information only. See `SDWebImageContext` for more detailed information. */ -FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderWebImageContext API_DEPRECATED("The coder component will be seperated from Core subspec in the future. Update your code to not rely on this context option.", macos(10.10, API_TO_BE_DEPRECATED), ios(8.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED));; +FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderWebImageContext API_DEPRECATED("The coder component will be seperated from Core subspec in the future. Update your code to not rely on this context option.", macos(10.10, API_TO_BE_DEPRECATED), ios(8.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED)); #pragma mark - Coder /** diff --git a/SDWebImage/Core/SDImageIOAnimatedCoder.m b/SDWebImage/Core/SDImageIOAnimatedCoder.m index 6fba3a66..bcb79c75 100644 --- a/SDWebImage/Core/SDImageIOAnimatedCoder.m +++ b/SDWebImage/Core/SDImageIOAnimatedCoder.m @@ -655,7 +655,7 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination return nil; } image.sd_imageFormat = self.class.imageFormat; - image.sd_isDecoded = YES;; + image.sd_isDecoded = YES; return image; } diff --git a/SDWebImage/Core/SDImageLoader.h b/SDWebImage/Core/SDImageLoader.h index 9f1c9524..25f1af40 100644 --- a/SDWebImage/Core/SDImageLoader.h +++ b/SDWebImage/Core/SDImageLoader.h @@ -60,6 +60,7 @@ FOUNDATION_EXPORT UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NS */ @protocol SDImageLoader +@required /** Whether current image loader supports to load the provide image URL. This will be checked every time a new image request come for loader. If this return NO, we will mark this image load as failed. If return YES, we will start to call `requestImageWithURL:options:context:progress:completed:`. @@ -67,8 +68,23 @@ FOUNDATION_EXPORT UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NS @param url The image URL to be loaded. @return YES to continue download, NO to stop download. */ -- (BOOL)canRequestImageForURL:(nullable NSURL *)url; +- (BOOL)canRequestImageForURL:(nullable NSURL *)url API_DEPRECATED("Use canRequestImageForURL:options:context: instead", macos(10.10, API_TO_BE_DEPRECATED), ios(8.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED)); +@optional +/** + Whether current image loader supports to load the provide image URL, with associated options and context. + This will be checked every time a new image request come for loader. If this return NO, we will mark this image load as failed. If return YES, we will start to call `requestImageWithURL:options:context:progress:completed:`. + + @param url The image URL to be loaded. + @param options A mask to specify options to use for this request + @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold. + @return YES to continue download, NO to stop download. + */ +- (BOOL)canRequestImageForURL:(nullable NSURL *)url + options:(SDWebImageOptions)options + context:(nullable SDWebImageContext *)context; + +@required /** Load the image and image data with the given URL and return the image data. You're responsible for producing the image instance. @@ -96,6 +112,22 @@ FOUNDATION_EXPORT UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NS @return Whether to block this url or not. Return YES to mark this URL as failed. */ - (BOOL)shouldBlockFailedURLWithURL:(nonnull NSURL *)url - error:(nonnull NSError *)error; + error:(nonnull NSError *)error API_DEPRECATED("Use shouldBlockFailedURLWithURL:error:options:context: instead", macos(10.10, API_TO_BE_DEPRECATED), ios(8.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED)); + +@optional +/** + Whether the error from image loader should be marked indeed un-recoverable or not, with associated options and context. + If this return YES, failed URL which does not using `SDWebImageRetryFailed` will be blocked into black list. Else not. + + @param url The URL represent the image. Note this may not be a HTTP URL + @param error The URL's loading error, from previous `requestImageWithURL:options:context:progress:completed:` completedBlock's error. + @param options A mask to specify options to use for this request + @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold. + @return Whether to block this url or not. Return YES to mark this URL as failed. + */ +- (BOOL)shouldBlockFailedURLWithURL:(nonnull NSURL *)url + error:(nonnull NSError *)error + options:(SDWebImageOptions)options + context:(nullable SDWebImageContext *)context; @end diff --git a/SDWebImage/Core/SDWebImageManager.m b/SDWebImage/Core/SDWebImageManager.m index f59dda10..0ad99a2b 100644 --- a/SDWebImage/Core/SDWebImageManager.m +++ b/SDWebImage/Core/SDWebImageManager.m @@ -378,7 +378,11 @@ static id _defaultImageLoader; BOOL shouldDownload = !SD_OPTIONS_CONTAINS(options, SDWebImageFromCacheOnly); shouldDownload &= (!cachedImage || options & SDWebImageRefreshCached); shouldDownload &= (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url]); - shouldDownload &= [imageLoader canRequestImageForURL:url]; + if ([imageLoader respondsToSelector:@selector(canRequestImageForURL:options:context:)]) { + shouldDownload &= [imageLoader canRequestImageForURL:url options:options context:context]; + } else { + shouldDownload &= [imageLoader canRequestImageForURL:url]; + } if (shouldDownload) { if (cachedImage && options & SDWebImageRefreshCached) { // If image was found in the cache but SDWebImageRefreshCached is provided, notify about the cached image @@ -631,7 +635,11 @@ static id _defaultImageLoader; if ([self.delegate respondsToSelector:@selector(imageManager:shouldBlockFailedURL:withError:)]) { shouldBlockFailedURL = [self.delegate imageManager:self shouldBlockFailedURL:url withError:error]; } else { - shouldBlockFailedURL = [imageLoader shouldBlockFailedURLWithURL:url error:error]; + if ([imageLoader respondsToSelector:@selector(shouldBlockFailedURLWithURL:error:options:context:)]) { + shouldBlockFailedURL = [imageLoader shouldBlockFailedURLWithURL:url error:error options:options context:context]; + } else { + shouldBlockFailedURL = [imageLoader shouldBlockFailedURLWithURL:url error:error]; + } } return shouldBlockFailedURL;