From 136d05fce8713155823aaaaa3aa721c3312a32d1 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 7 Nov 2022 12:42:32 +0800 Subject: [PATCH] Add extra check when user provide SDImageCoderDecodeUseLazyDecoding --- SDWebImage/Core/SDImageCacheDefine.m | 6 +++++- SDWebImage/Core/SDImageLoader.m | 17 ++++++++++++----- SDWebImage/Core/SDWebImageDefine.h | 3 ++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/SDWebImage/Core/SDImageCacheDefine.m b/SDWebImage/Core/SDImageCacheDefine.m index 7278c6d6..db3f43fa 100644 --- a/SDWebImage/Core/SDImageCacheDefine.m +++ b/SDWebImage/Core/SDImageCacheDefine.m @@ -116,7 +116,11 @@ UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSS } if (image) { BOOL shouldDecode = !SD_OPTIONS_CONTAINS(options, SDWebImageAvoidDecodeImage); - if ([image.class conformsToProtocol:@protocol(SDAnimatedImage)]) { + BOOL lazyDecode = [coderOptions[SDImageCoderDecodeUseLazyDecoding] boolValue]; + if (lazyDecode) { + // lazyDecode = NO means we should not forceDecode, highest priority + shouldDecode = NO; + } else if ([image.class conformsToProtocol:@protocol(SDAnimatedImage)]) { // `SDAnimatedImage` do not decode shouldDecode = NO; } else if (image.sd_isAnimated) { diff --git a/SDWebImage/Core/SDImageLoader.m b/SDWebImage/Core/SDImageLoader.m index 03143b0b..c8148d8b 100644 --- a/SDWebImage/Core/SDImageLoader.m +++ b/SDWebImage/Core/SDImageLoader.m @@ -77,14 +77,17 @@ UIImage * _Nullable SDImageLoaderDecodeImageData(NSData * _Nonnull imageData, NS } if (image) { BOOL shouldDecode = !SD_OPTIONS_CONTAINS(options, SDWebImageAvoidDecodeImage); - if ([image.class conformsToProtocol:@protocol(SDAnimatedImage)]) { + BOOL lazyDecode = [coderOptions[SDImageCoderDecodeUseLazyDecoding] boolValue]; + if (lazyDecode) { + // lazyDecode = NO means we should not forceDecode, highest priority + shouldDecode = NO; + } else if ([image.class conformsToProtocol:@protocol(SDAnimatedImage)]) { // `SDAnimatedImage` do not decode shouldDecode = NO; } else if (image.sd_isAnimated) { // animated image do not decode shouldDecode = NO; } - if (shouldDecode) { image = [SDImageCoderHelper decodedImageWithImage:image]; } @@ -157,7 +160,11 @@ UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull im } if (image) { BOOL shouldDecode = !SD_OPTIONS_CONTAINS(options, SDWebImageAvoidDecodeImage); - if ([image.class conformsToProtocol:@protocol(SDAnimatedImage)]) { + BOOL lazyDecode = [coderOptions[SDImageCoderDecodeUseLazyDecoding] boolValue]; + if (lazyDecode) { + // lazyDecode = NO means we should not forceDecode, highest priority + shouldDecode = NO; + } else if ([image.class conformsToProtocol:@protocol(SDAnimatedImage)]) { // `SDAnimatedImage` do not decode shouldDecode = NO; } else if (image.sd_isAnimated) { @@ -167,10 +174,10 @@ UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull im if (shouldDecode) { image = [SDImageCoderHelper decodedImageWithImage:image]; } - // mark the image as progressive (completed one are not mark as progressive) - image.sd_isIncremental = !finished; // assign the decode options, to let manager check whether to re-decode if needed image.sd_decodeOptions = coderOptions; + // mark the image as progressive (completed one are not mark as progressive) + image.sd_isIncremental = !finished; } return image; diff --git a/SDWebImage/Core/SDWebImageDefine.h b/SDWebImage/Core/SDWebImageDefine.h index 88f2733e..df7c7fa4 100644 --- a/SDWebImage/Core/SDWebImageDefine.h +++ b/SDWebImage/Core/SDWebImageDefine.h @@ -169,7 +169,8 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) { /** * By default, we will decode the image in the background during cache query and download from the network. This can help to improve performance because when rendering image on the screen, it need to be firstly decoded. But this happen on the main queue by Core Animation. - * However, this process may increase the memory usage as well. If you are experiencing a issue due to excessive memory consumption, This flag can prevent decode the image. + * However, this process may increase the memory usage as well. If you are experiencing an issue due to excessive memory consumption, This flag can prevent decode the image. + * @note 5.14.0 introduce `SDImageCoderDecodeUseLazyDecoding`, use that for better control from codec, instead of post-processing. Which acts the similar like this option but works for SDAnimatedImage as well (this one does not) */ SDWebImageAvoidDecodeImage = 1 << 18,