From bfa91a291bd530048f8546e782d9f45941550e55 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Tue, 8 Nov 2022 22:39:57 +0800 Subject: [PATCH] Added the helper convenient API `sd_isThumbnail` Fix the metadata does not saved --- SDWebImage/Core/SDWebImageManager.m | 13 ++----------- SDWebImage/Core/UIImage+Metadata.h | 6 ++++++ SDWebImage/Core/UIImage+Metadata.m | 13 +++++++++++++ SDWebImage/Private/SDAssociatedObject.m | 1 + 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/SDWebImage/Core/SDWebImageManager.m b/SDWebImage/Core/SDWebImageManager.m index a0f1f118..00b99573 100644 --- a/SDWebImage/Core/SDWebImageManager.m +++ b/SDWebImage/Core/SDWebImageManager.m @@ -489,19 +489,10 @@ static id _defaultImageLoader; shouldTransformImage = shouldTransformImage && (!originalImage.sd_isAnimated || (options & SDWebImageTransformAnimatedImage)); shouldTransformImage = shouldTransformImage && (!originalImage.sd_isVector || (options & SDWebImageTransformVectorImage)); // thumbnail check - CGSize thumbnailSize = CGSizeZero; - NSValue *thumbnailSizeValue = originalImage.sd_decodeOptions[SDImageCoderDecodeThumbnailPixelSize]; - if (thumbnailSizeValue != nil) { -#if SD_MAC - thumbnailSize = thumbnailSizeValue.sizeValue; -#else - thumbnailSize = thumbnailSizeValue.CGSizeValue; -#endif - } - BOOL shouldEncodeThumbnail = thumbnailSize.width > 0 && thumbnailSize.height > 0; + BOOL isThumbnail = originalImage.sd_isThumbnail; NSData *cacheData = originalData; UIImage *cacheImage = originalImage; - if (shouldEncodeThumbnail) { + if (isThumbnail) { cacheData = nil; // thumbnail don't store full size data originalImage = nil; // thumbnail don't have full size image } diff --git a/SDWebImage/Core/UIImage+Metadata.h b/SDWebImage/Core/UIImage+Metadata.h index 46ee3f85..5caf5510 100644 --- a/SDWebImage/Core/UIImage+Metadata.h +++ b/SDWebImage/Core/UIImage+Metadata.h @@ -71,6 +71,12 @@ */ @property (nonatomic, assign) BOOL sd_isTransformed; +/** + A bool value indicating that the image is using thumbnail decode with smaller size, so the image data may not always match original download one. + @note This just check `sd_decodeOptions[.decodeThumbnailPixelSize] > CGSize.zero` + */ +@property (nonatomic, assign, readonly) BOOL sd_isThumbnail; + /** A dictionary value contains the decode options when decoded from SDWebImage loading system (say, `SDImageCacheDecodeImageData/SDImageLoaderDecode[Progressive]ImageData`) It may not always available and only image decoding related options will be saved. (including [.decodeScaleFactor, .decodeThumbnailPixelSize, .decodePreserveAspectRatio, .decodeFirstFrameOnly]) diff --git a/SDWebImage/Core/UIImage+Metadata.m b/SDWebImage/Core/UIImage+Metadata.m index a7c7c529..8a8e07ea 100644 --- a/SDWebImage/Core/UIImage+Metadata.m +++ b/SDWebImage/Core/UIImage+Metadata.m @@ -197,6 +197,19 @@ objc_setAssociatedObject(self, @selector(sd_decodeOptions), sd_decodeOptions, OBJC_ASSOCIATION_COPY_NONATOMIC); } +-(BOOL)sd_isThumbnail { + CGSize thumbnailSize = CGSizeZero; + NSValue *thumbnailSizeValue = self.sd_decodeOptions[SDImageCoderDecodeThumbnailPixelSize]; + if (thumbnailSizeValue != nil) { + #if SD_MAC + thumbnailSize = thumbnailSizeValue.sizeValue; + #else + thumbnailSize = thumbnailSizeValue.CGSizeValue; + #endif + } + return thumbnailSize.width > 0 && thumbnailSize.height > 0; +} + - (SDImageCoderOptions *)sd_decodeOptions { SDImageCoderOptions *value = objc_getAssociatedObject(self, @selector(sd_decodeOptions)); if ([value isKindOfClass:NSDictionary.class]) { diff --git a/SDWebImage/Private/SDAssociatedObject.m b/SDWebImage/Private/SDAssociatedObject.m index c122da4d..4aff1e0c 100644 --- a/SDWebImage/Private/SDAssociatedObject.m +++ b/SDWebImage/Private/SDAssociatedObject.m @@ -18,6 +18,7 @@ void SDImageCopyAssociatedObject(UIImage * _Nullable source, UIImage * _Nullable } // Image Metadata target.sd_isIncremental = source.sd_isIncremental; + target.sd_isTransformed = source.sd_isTransformed; target.sd_decodeOptions = source.sd_decodeOptions; target.sd_imageLoopCount = source.sd_imageLoopCount; target.sd_imageFormat = source.sd_imageFormat;