From 9e5ef8c0e9fef0c88513be257e511b45c7dc5dbf Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 6 Jan 2020 18:54:01 +0800 Subject: [PATCH] Change all the force decode method to be compatible with macOS. Update the documentation as well --- SDWebImage/Core/SDImageCacheDefine.m | 9 +-------- SDWebImage/Core/SDImageCoderHelper.h | 2 +- SDWebImage/Core/SDImageCoderHelper.m | 25 +++++++++++-------------- SDWebImage/Core/SDImageLoader.m | 4 ---- SDWebImage/Core/SDWebImageDefine.h | 4 ++-- 5 files changed, 15 insertions(+), 29 deletions(-) diff --git a/SDWebImage/Core/SDImageCacheDefine.m b/SDWebImage/Core/SDImageCacheDefine.m index d13c3c39..75dfb4e6 100644 --- a/SDWebImage/Core/SDImageCacheDefine.m +++ b/SDWebImage/Core/SDImageCacheDefine.m @@ -20,14 +20,12 @@ UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSS CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); NSNumber *preserveAspectRatioValue = context[SDWebImageContextImagePreserveAspectRatio]; NSValue *thumbnailSizeValue; -#if SD_UIKIT || SD_WATCH BOOL shouldScaleDown = SD_OPTIONS_CONTAINS(options, SDWebImageScaleDownLargeImages); if (shouldScaleDown) { CGFloat thumbnailPixels = SDImageCoderHelper.defaultScaleDownLimitBytes / 4; CGFloat dimension = ceil(sqrt(thumbnailPixels)); thumbnailSizeValue = @(CGSizeMake(dimension, dimension)); } -#endif if (context[SDWebImageContextImageThumbnailPixelSize]) { thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize]; } @@ -71,12 +69,7 @@ UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSS shouldDecode = NO; } if (shouldDecode) { - BOOL shouldScaleDown = SD_OPTIONS_CONTAINS(options, SDWebImageScaleDownLargeImages); - if (shouldScaleDown) { - image = [SDImageCoderHelper decodedAndScaledDownImageWithImage:image limitBytes:0]; - } else { - image = [SDImageCoderHelper decodedImageWithImage:image]; - } + image = [SDImageCoderHelper decodedImageWithImage:image]; } } diff --git a/SDWebImage/Core/SDImageCoderHelper.h b/SDWebImage/Core/SDImageCoderHelper.h index 9df53e0c..353a8729 100644 --- a/SDWebImage/Core/SDImageCoderHelper.h +++ b/SDWebImage/Core/SDImageCoderHelper.h @@ -89,13 +89,13 @@ */ + (UIImage * _Nullable)decodedAndScaledDownImageWithImage:(UIImage * _Nullable)image limitBytes:(NSUInteger)bytes; -#if SD_UIKIT || SD_WATCH /** Control the default limit bytes to scale down larget images. This value must be larger than or equal to 1MB. Defaults to 60MB. */ @property (class, readwrite) NSUInteger defaultScaleDownLimitBytes; +#if SD_UIKIT || SD_WATCH /** Convert an EXIF image orientation to an iOS one. diff --git a/SDWebImage/Core/SDImageCoderHelper.m b/SDWebImage/Core/SDImageCoderHelper.m index 9e92dd60..ac671366 100644 --- a/SDWebImage/Core/SDImageCoderHelper.m +++ b/SDWebImage/Core/SDImageCoderHelper.m @@ -13,8 +13,8 @@ #import "SDAnimatedImageRep.h" #import "UIImage+ForceDecode.h" #import "SDAssociatedObject.h" +#import "UIImage+Metadata.h" -#if SD_UIKIT || SD_WATCH static const size_t kBytesPerPixel = 4; static const size_t kBitsPerComponent = 8; @@ -29,7 +29,6 @@ static const CGFloat kPixelsPerMB = kBytesPerMB / kBytesPerPixel; static CGFloat kDestImageLimitBytes = 60.f * kBytesPerMB; static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to overlap the seems where tiles meet. -#endif @implementation SDImageCoderHelper @@ -267,9 +266,6 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over } + (UIImage *)decodedImageWithImage:(UIImage *)image { -#if SD_MAC - return image; -#else if (![self shouldDecodeImage:image]) { return image; } @@ -278,18 +274,18 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over if (!imageRef) { return image; } +#if SD_MAC + UIImage *decodedImage = [[UIImage alloc] initWithCGImage:imageRef scale:image.scale orientation:kCGImagePropertyOrientationUp]; +#else UIImage *decodedImage = [[UIImage alloc] initWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation]; +#endif CGImageRelease(imageRef); SDImageCopyAssociatedObject(image, decodedImage); decodedImage.sd_isDecoded = YES; return decodedImage; -#endif } + (UIImage *)decodedAndScaledDownImageWithImage:(UIImage *)image limitBytes:(NSUInteger)bytes { -#if SD_MAC - return image; -#else if (![self shouldDecodeImage:image]) { return image; } @@ -407,7 +403,11 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over if (destImageRef == NULL) { return image; } +#if SD_MAC + UIImage *destImage = [[UIImage alloc] initWithCGImage:destImageRef scale:image.scale orientation:kCGImagePropertyOrientationUp]; +#else UIImage *destImage = [[UIImage alloc] initWithCGImage:destImageRef scale:image.scale orientation:image.imageOrientation]; +#endif CGImageRelease(destImageRef); if (destImage == nil) { return image; @@ -416,10 +416,8 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over destImage.sd_isDecoded = YES; return destImage; } -#endif } -#if SD_UIKIT || SD_WATCH + (NSUInteger)defaultScaleDownLimitBytes { return kDestImageLimitBytes; } @@ -431,6 +429,7 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over kDestImageLimitBytes = defaultScaleDownLimitBytes; } +#if SD_UIKIT || SD_WATCH // Convert an EXIF image orientation to an iOS one. + (UIImageOrientation)imageOrientationFromEXIFOrientation:(CGImagePropertyOrientation)exifOrientation { UIImageOrientation imageOrientation = UIImageOrientationUp; @@ -501,7 +500,6 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over #endif #pragma mark - Helper Fuction -#if SD_UIKIT || SD_WATCH + (BOOL)shouldDecodeImage:(nullable UIImage *)image { // Avoid extra decode if (image.sd_isDecoded) { @@ -512,7 +510,7 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over return NO; } // do not decode animated images - if (image.images != nil) { + if (image.sd_isAnimated) { return NO; } @@ -548,7 +546,6 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over return shouldScaleDown; } -#endif static inline CGAffineTransform SDCGContextTransformFromOrientation(CGImagePropertyOrientation orientation, CGSize size) { // Inspiration from @libfeihu diff --git a/SDWebImage/Core/SDImageLoader.m b/SDWebImage/Core/SDImageLoader.m index 2bd48b15..4c831c59 100644 --- a/SDWebImage/Core/SDImageLoader.m +++ b/SDWebImage/Core/SDImageLoader.m @@ -34,14 +34,12 @@ UIImage * _Nullable SDImageLoaderDecodeImageData(NSData * _Nonnull imageData, NS CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); NSNumber *preserveAspectRatioValue = context[SDWebImageContextImagePreserveAspectRatio]; NSValue *thumbnailSizeValue; -#if SD_UIKIT || SD_WATCH BOOL shouldScaleDown = SD_OPTIONS_CONTAINS(options, SDWebImageScaleDownLargeImages); if (shouldScaleDown) { CGFloat thumbnailPixels = SDImageCoderHelper.defaultScaleDownLimitBytes / 4; CGFloat dimension = ceil(sqrt(thumbnailPixels)); thumbnailSizeValue = @(CGSizeMake(dimension, dimension)); } -#endif if (context[SDWebImageContextImageThumbnailPixelSize]) { thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize]; } @@ -111,14 +109,12 @@ UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull im CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); NSNumber *preserveAspectRatioValue = context[SDWebImageContextImagePreserveAspectRatio]; NSValue *thumbnailSizeValue; -#if SD_UIKIT || SD_WATCH BOOL shouldScaleDown = SD_OPTIONS_CONTAINS(options, SDWebImageScaleDownLargeImages); if (shouldScaleDown) { CGFloat thumbnailPixels = SDImageCoderHelper.defaultScaleDownLimitBytes / 4; CGFloat dimension = ceil(sqrt(thumbnailPixels)); thumbnailSizeValue = @(CGSizeMake(dimension, dimension)); } -#endif if (context[SDWebImageContextImageThumbnailPixelSize]) { thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize]; } diff --git a/SDWebImage/Core/SDWebImageDefine.h b/SDWebImage/Core/SDWebImageDefine.h index 549c1da7..8c364844 100644 --- a/SDWebImage/Core/SDWebImageDefine.h +++ b/SDWebImage/Core/SDWebImageDefine.h @@ -123,8 +123,8 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) { /** * By default, images are decoded respecting their original size. - * On iOS/tvOS/watchOS, this flag will scale down the images to a size compatible with the constrained memory of devices. On macOS, this does nothing. - * To control the limit bytes, check `SDImageCoderHelper.defaultScaleDownLimitBytes` (Defaults to 60MB) + * This flag will scale down the images to a size compatible with the constrained memory of devices. + * To control the limit memory bytes, check `SDImageCoderHelper.defaultScaleDownLimitBytes` (Defaults to 60MB) * This will actually translate to use context option `.imageThumbnailPixelSize` from v5.5.0 (Defaults to (3966, 3966)). Previously does not. * This flags effect the progressive and animated images as well from v5.5.0. Previously does not. * @note If you need detail controls, it's better to use context option `imageThumbnailPixelSize` and `imagePreserveAspectRatio` instead.