Change all the force decode method to be compatible with macOS. Update the documentation as well

This commit is contained in:
DreamPiggy 2020-01-06 18:54:01 +08:00
parent ec620438b9
commit 9e5ef8c0e9
5 changed files with 15 additions and 29 deletions

View File

@ -20,14 +20,12 @@ UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSS
CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey);
NSNumber *preserveAspectRatioValue = context[SDWebImageContextImagePreserveAspectRatio]; NSNumber *preserveAspectRatioValue = context[SDWebImageContextImagePreserveAspectRatio];
NSValue *thumbnailSizeValue; NSValue *thumbnailSizeValue;
#if SD_UIKIT || SD_WATCH
BOOL shouldScaleDown = SD_OPTIONS_CONTAINS(options, SDWebImageScaleDownLargeImages); BOOL shouldScaleDown = SD_OPTIONS_CONTAINS(options, SDWebImageScaleDownLargeImages);
if (shouldScaleDown) { if (shouldScaleDown) {
CGFloat thumbnailPixels = SDImageCoderHelper.defaultScaleDownLimitBytes / 4; CGFloat thumbnailPixels = SDImageCoderHelper.defaultScaleDownLimitBytes / 4;
CGFloat dimension = ceil(sqrt(thumbnailPixels)); CGFloat dimension = ceil(sqrt(thumbnailPixels));
thumbnailSizeValue = @(CGSizeMake(dimension, dimension)); thumbnailSizeValue = @(CGSizeMake(dimension, dimension));
} }
#endif
if (context[SDWebImageContextImageThumbnailPixelSize]) { if (context[SDWebImageContextImageThumbnailPixelSize]) {
thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize]; thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize];
} }
@ -71,14 +69,9 @@ UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSS
shouldDecode = NO; shouldDecode = NO;
} }
if (shouldDecode) { 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];
} }
} }
}
return image; return image;
} }

View File

@ -89,13 +89,13 @@
*/ */
+ (UIImage * _Nullable)decodedAndScaledDownImageWithImage:(UIImage * _Nullable)image limitBytes:(NSUInteger)bytes; + (UIImage * _Nullable)decodedAndScaledDownImageWithImage:(UIImage * _Nullable)image limitBytes:(NSUInteger)bytes;
#if SD_UIKIT || SD_WATCH
/** /**
Control the default limit bytes to scale down larget images. Control the default limit bytes to scale down larget images.
This value must be larger than or equal to 1MB. Defaults to 60MB. This value must be larger than or equal to 1MB. Defaults to 60MB.
*/ */
@property (class, readwrite) NSUInteger defaultScaleDownLimitBytes; @property (class, readwrite) NSUInteger defaultScaleDownLimitBytes;
#if SD_UIKIT || SD_WATCH
/** /**
Convert an EXIF image orientation to an iOS one. Convert an EXIF image orientation to an iOS one.

View File

@ -13,8 +13,8 @@
#import "SDAnimatedImageRep.h" #import "SDAnimatedImageRep.h"
#import "UIImage+ForceDecode.h" #import "UIImage+ForceDecode.h"
#import "SDAssociatedObject.h" #import "SDAssociatedObject.h"
#import "UIImage+Metadata.h"
#if SD_UIKIT || SD_WATCH
static const size_t kBytesPerPixel = 4; static const size_t kBytesPerPixel = 4;
static const size_t kBitsPerComponent = 8; static const size_t kBitsPerComponent = 8;
@ -29,7 +29,6 @@ static const CGFloat kPixelsPerMB = kBytesPerMB / kBytesPerPixel;
static CGFloat kDestImageLimitBytes = 60.f * kBytesPerMB; static CGFloat kDestImageLimitBytes = 60.f * kBytesPerMB;
static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to overlap the seems where tiles meet. static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to overlap the seems where tiles meet.
#endif
@implementation SDImageCoderHelper @implementation SDImageCoderHelper
@ -267,9 +266,6 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
} }
+ (UIImage *)decodedImageWithImage:(UIImage *)image { + (UIImage *)decodedImageWithImage:(UIImage *)image {
#if SD_MAC
return image;
#else
if (![self shouldDecodeImage:image]) { if (![self shouldDecodeImage:image]) {
return image; return image;
} }
@ -278,18 +274,18 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
if (!imageRef) { if (!imageRef) {
return image; 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]; UIImage *decodedImage = [[UIImage alloc] initWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
#endif
CGImageRelease(imageRef); CGImageRelease(imageRef);
SDImageCopyAssociatedObject(image, decodedImage); SDImageCopyAssociatedObject(image, decodedImage);
decodedImage.sd_isDecoded = YES; decodedImage.sd_isDecoded = YES;
return decodedImage; return decodedImage;
#endif
} }
+ (UIImage *)decodedAndScaledDownImageWithImage:(UIImage *)image limitBytes:(NSUInteger)bytes { + (UIImage *)decodedAndScaledDownImageWithImage:(UIImage *)image limitBytes:(NSUInteger)bytes {
#if SD_MAC
return image;
#else
if (![self shouldDecodeImage:image]) { if (![self shouldDecodeImage:image]) {
return image; return image;
} }
@ -407,7 +403,11 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
if (destImageRef == NULL) { if (destImageRef == NULL) {
return image; 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]; UIImage *destImage = [[UIImage alloc] initWithCGImage:destImageRef scale:image.scale orientation:image.imageOrientation];
#endif
CGImageRelease(destImageRef); CGImageRelease(destImageRef);
if (destImage == nil) { if (destImage == nil) {
return image; return image;
@ -416,10 +416,8 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
destImage.sd_isDecoded = YES; destImage.sd_isDecoded = YES;
return destImage; return destImage;
} }
#endif
} }
#if SD_UIKIT || SD_WATCH
+ (NSUInteger)defaultScaleDownLimitBytes { + (NSUInteger)defaultScaleDownLimitBytes {
return kDestImageLimitBytes; return kDestImageLimitBytes;
} }
@ -431,6 +429,7 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
kDestImageLimitBytes = defaultScaleDownLimitBytes; kDestImageLimitBytes = defaultScaleDownLimitBytes;
} }
#if SD_UIKIT || SD_WATCH
// Convert an EXIF image orientation to an iOS one. // Convert an EXIF image orientation to an iOS one.
+ (UIImageOrientation)imageOrientationFromEXIFOrientation:(CGImagePropertyOrientation)exifOrientation { + (UIImageOrientation)imageOrientationFromEXIFOrientation:(CGImagePropertyOrientation)exifOrientation {
UIImageOrientation imageOrientation = UIImageOrientationUp; UIImageOrientation imageOrientation = UIImageOrientationUp;
@ -501,7 +500,6 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
#endif #endif
#pragma mark - Helper Fuction #pragma mark - Helper Fuction
#if SD_UIKIT || SD_WATCH
+ (BOOL)shouldDecodeImage:(nullable UIImage *)image { + (BOOL)shouldDecodeImage:(nullable UIImage *)image {
// Avoid extra decode // Avoid extra decode
if (image.sd_isDecoded) { if (image.sd_isDecoded) {
@ -512,7 +510,7 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
return NO; return NO;
} }
// do not decode animated images // do not decode animated images
if (image.images != nil) { if (image.sd_isAnimated) {
return NO; return NO;
} }
@ -548,7 +546,6 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
return shouldScaleDown; return shouldScaleDown;
} }
#endif
static inline CGAffineTransform SDCGContextTransformFromOrientation(CGImagePropertyOrientation orientation, CGSize size) { static inline CGAffineTransform SDCGContextTransformFromOrientation(CGImagePropertyOrientation orientation, CGSize size) {
// Inspiration from @libfeihu // Inspiration from @libfeihu

View File

@ -34,14 +34,12 @@ UIImage * _Nullable SDImageLoaderDecodeImageData(NSData * _Nonnull imageData, NS
CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey);
NSNumber *preserveAspectRatioValue = context[SDWebImageContextImagePreserveAspectRatio]; NSNumber *preserveAspectRatioValue = context[SDWebImageContextImagePreserveAspectRatio];
NSValue *thumbnailSizeValue; NSValue *thumbnailSizeValue;
#if SD_UIKIT || SD_WATCH
BOOL shouldScaleDown = SD_OPTIONS_CONTAINS(options, SDWebImageScaleDownLargeImages); BOOL shouldScaleDown = SD_OPTIONS_CONTAINS(options, SDWebImageScaleDownLargeImages);
if (shouldScaleDown) { if (shouldScaleDown) {
CGFloat thumbnailPixels = SDImageCoderHelper.defaultScaleDownLimitBytes / 4; CGFloat thumbnailPixels = SDImageCoderHelper.defaultScaleDownLimitBytes / 4;
CGFloat dimension = ceil(sqrt(thumbnailPixels)); CGFloat dimension = ceil(sqrt(thumbnailPixels));
thumbnailSizeValue = @(CGSizeMake(dimension, dimension)); thumbnailSizeValue = @(CGSizeMake(dimension, dimension));
} }
#endif
if (context[SDWebImageContextImageThumbnailPixelSize]) { if (context[SDWebImageContextImageThumbnailPixelSize]) {
thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize]; thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize];
} }
@ -111,14 +109,12 @@ UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull im
CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey);
NSNumber *preserveAspectRatioValue = context[SDWebImageContextImagePreserveAspectRatio]; NSNumber *preserveAspectRatioValue = context[SDWebImageContextImagePreserveAspectRatio];
NSValue *thumbnailSizeValue; NSValue *thumbnailSizeValue;
#if SD_UIKIT || SD_WATCH
BOOL shouldScaleDown = SD_OPTIONS_CONTAINS(options, SDWebImageScaleDownLargeImages); BOOL shouldScaleDown = SD_OPTIONS_CONTAINS(options, SDWebImageScaleDownLargeImages);
if (shouldScaleDown) { if (shouldScaleDown) {
CGFloat thumbnailPixels = SDImageCoderHelper.defaultScaleDownLimitBytes / 4; CGFloat thumbnailPixels = SDImageCoderHelper.defaultScaleDownLimitBytes / 4;
CGFloat dimension = ceil(sqrt(thumbnailPixels)); CGFloat dimension = ceil(sqrt(thumbnailPixels));
thumbnailSizeValue = @(CGSizeMake(dimension, dimension)); thumbnailSizeValue = @(CGSizeMake(dimension, dimension));
} }
#endif
if (context[SDWebImageContextImageThumbnailPixelSize]) { if (context[SDWebImageContextImageThumbnailPixelSize]) {
thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize]; thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize];
} }

View File

@ -123,8 +123,8 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
/** /**
* By default, images are decoded respecting their original size. * 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. * This flag will scale down the images to a size compatible with the constrained memory of devices.
* To control the limit bytes, check `SDImageCoderHelper.defaultScaleDownLimitBytes` (Defaults to 60MB) * 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 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. * 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. * @note If you need detail controls, it's better to use context option `imageThumbnailPixelSize` and `imagePreserveAspectRatio` instead.