From 806e2288671decaff7df1dd734fa1d6851381d21 Mon Sep 17 00:00:00 2001 From: kinarob Date: Fri, 26 Apr 2019 16:35:54 +0800 Subject: [PATCH] Replace if judge with MAX() function in scale less 1 --- SDWebImage/SDAnimatedImage.m | 7 ++----- SDWebImage/SDImageAPNGCoder.m | 20 ++++---------------- SDWebImage/SDImageCacheDefine.m | 5 +---- SDWebImage/SDImageCoderHelper.m | 6 ++---- SDWebImage/SDImageGIFCoder.m | 20 ++++---------------- SDWebImage/SDImageIOCoder.m | 15 +++------------ SDWebImage/SDImageLoader.m | 10 ++-------- SDWebImage/UIImage+MultiFormat.m | 5 +---- 8 files changed, 19 insertions(+), 69 deletions(-) diff --git a/SDWebImage/SDAnimatedImage.m b/SDWebImage/SDAnimatedImage.m index 9fbd13e3..7d9ed01a 100644 --- a/SDWebImage/SDAnimatedImage.m +++ b/SDWebImage/SDAnimatedImage.m @@ -146,17 +146,14 @@ static CGFloat SDImageScaleFromPath(NSString *string) { if (!animatedCoder) { return nil; } - if (scale <= 0) { - scale = 1; - } UIImage *image = [animatedCoder animatedImageFrameAtIndex:0]; if (!image) { return nil; } #if SD_MAC - self = [super initWithCGImage:image.CGImage scale:scale orientation:kCGImagePropertyOrientationUp]; + self = [super initWithCGImage:image.CGImage scale:MAX(scale, 1) orientation:kCGImagePropertyOrientationUp]; #else - self = [super initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation]; + self = [super initWithCGImage:image.CGImage scale:MAX(scale, 1) orientation:image.imageOrientation]; #endif if (self) { _coder = animatedCoder; diff --git a/SDWebImage/SDImageAPNGCoder.m b/SDWebImage/SDImageAPNGCoder.m index 09b49f72..d816bcc2 100644 --- a/SDWebImage/SDImageAPNGCoder.m +++ b/SDWebImage/SDImageAPNGCoder.m @@ -83,10 +83,7 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef CGFloat scale = 1; NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; if (scaleFactor != nil) { - scale = [scaleFactor doubleValue]; - if (scale < 1) { - scale = 1; - } + scale = MAX([scaleFactor doubleValue], 1); } #if SD_MAC @@ -252,10 +249,7 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef CGFloat scale = 1; NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; if (scaleFactor != nil) { - scale = [scaleFactor doubleValue]; - if (scale < 1) { - scale = 1; - } + scale = MAX([scaleFactor doubleValue], 1); } _scale = scale; #if SD_UIKIT @@ -304,10 +298,7 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef CGFloat scale = _scale; NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; if (scaleFactor != nil) { - scale = [scaleFactor doubleValue]; - if (scale < 1) { - scale = 1; - } + scale = MAX([scaleFactor doubleValue], 1); } #if SD_UIKIT || SD_WATCH image = [[UIImage alloc] initWithCGImage:partialImageRef scale:scale orientation:UIImageOrientationUp]; @@ -341,10 +332,7 @@ const CFStringRef kCGImagePropertyAPNGUnclampedDelayTime = (__bridge CFStringRef CGFloat scale = 1; NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; if (scaleFactor != nil) { - scale = [scaleFactor doubleValue]; - if (scale < 1) { - scale = 1; - } + scale = MAX([scaleFactor doubleValue], 1); } _scale = scale; _imageSource = imageSource; diff --git a/SDWebImage/SDImageCacheDefine.m b/SDWebImage/SDImageCacheDefine.m index 423e0583..61e00183 100644 --- a/SDWebImage/SDImageCacheDefine.m +++ b/SDWebImage/SDImageCacheDefine.m @@ -17,10 +17,7 @@ UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSS BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly; NSNumber *scaleValue = context[SDWebImageContextImageScaleFactor]; CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); - if (scale < 1) { - scale = 1; - } - SDImageCoderOptions *coderOptions = @{SDImageCoderDecodeFirstFrameOnly : @(decodeFirstFrame), SDImageCoderDecodeScaleFactor : @(scale)}; + SDImageCoderOptions *coderOptions = @{SDImageCoderDecodeFirstFrameOnly : @(decodeFirstFrame), SDImageCoderDecodeScaleFactor : @(MAX(scale, 1))}; if (context) { SDImageCoderMutableOptions *mutableCoderOptions = [coderOptions mutableCopy]; [mutableCoderOptions setValue:context forKey:SDImageCoderWebImageContext]; diff --git a/SDWebImage/SDImageCoderHelper.m b/SDWebImage/SDImageCoderHelper.m index 99e523d5..b8ac0f77 100644 --- a/SDWebImage/SDImageCoderHelper.m +++ b/SDWebImage/SDImageCoderHelper.m @@ -104,10 +104,8 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over return nil; } CFRelease(imageDestination); - CGFloat scale = frames.firstObject.image.scale; - if (scale < 1) { - scale = 1; - } + CGFloat scale = MAX(frames.firstObject.image.scale, 1); + SDAnimatedImageRep *imageRep = [[SDAnimatedImageRep alloc] initWithData:imageData]; NSSize size = NSMakeSize(imageRep.pixelsWide / scale, imageRep.pixelsHigh / scale); imageRep.size = size; diff --git a/SDWebImage/SDImageGIFCoder.m b/SDWebImage/SDImageGIFCoder.m index 2bea5b94..6668c20a 100644 --- a/SDWebImage/SDImageGIFCoder.m +++ b/SDWebImage/SDImageGIFCoder.m @@ -76,10 +76,7 @@ CGFloat scale = 1; NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; if (scaleFactor != nil) { - scale = [scaleFactor doubleValue]; - if (scale < 1) { - scale = 1; - } + scale = MAX([scaleFactor doubleValue], 1); } #if SD_MAC @@ -189,10 +186,7 @@ CGFloat scale = 1; NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; if (scaleFactor != nil) { - scale = [scaleFactor doubleValue]; - if (scale < 1) { - scale = 1; - } + scale = MAX([scaleFactor doubleValue], 1); } _scale = scale; #if SD_UIKIT @@ -241,10 +235,7 @@ CGFloat scale = _scale; NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; if (scaleFactor != nil) { - scale = [scaleFactor doubleValue]; - if (scale < 1) { - scale = 1; - } + scale = MAX([scaleFactor doubleValue], 1); } #if SD_UIKIT || SD_WATCH image = [[UIImage alloc] initWithCGImage:partialImageRef scale:scale orientation:UIImageOrientationUp]; @@ -339,10 +330,7 @@ CGFloat scale = 1; NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; if (scaleFactor != nil) { - scale = [scaleFactor doubleValue]; - if (scale < 1) { - scale = 1; - } + scale = MAX([scaleFactor doubleValue], 1); } _scale = scale; _imageSource = imageSource; diff --git a/SDWebImage/SDImageIOCoder.m b/SDWebImage/SDImageIOCoder.m index 7d5d6bf4..54666696 100644 --- a/SDWebImage/SDImageIOCoder.m +++ b/SDWebImage/SDImageIOCoder.m @@ -70,10 +70,7 @@ CGFloat scale = 1; NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; if (scaleFactor != nil) { - scale = [scaleFactor doubleValue]; - if (scale < 1) { - scale = 1; - } + scale = MAX([scaleFactor doubleValue], 1) ; } UIImage *image = [[UIImage alloc] initWithData:data scale:scale]; @@ -94,10 +91,7 @@ CGFloat scale = 1; NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; if (scaleFactor != nil) { - scale = [scaleFactor doubleValue]; - if (scale < 1) { - scale = 1; - } + scale = MAX([scaleFactor doubleValue], 1); } _scale = scale; #if SD_UIKIT @@ -151,10 +145,7 @@ CGFloat scale = _scale; NSNumber *scaleFactor = options[SDImageCoderDecodeScaleFactor]; if (scaleFactor != nil) { - scale = [scaleFactor doubleValue]; - if (scale < 1) { - scale = 1; - } + scale = MAX([scaleFactor doubleValue], 1); } #if SD_UIKIT || SD_WATCH UIImageOrientation imageOrientation = [SDImageCoderHelper imageOrientationFromEXIFOrientation:_orientation]; diff --git a/SDWebImage/SDImageLoader.m b/SDWebImage/SDImageLoader.m index 7d3db1a9..d8343b50 100644 --- a/SDWebImage/SDImageLoader.m +++ b/SDWebImage/SDImageLoader.m @@ -31,10 +31,7 @@ UIImage * _Nullable SDImageLoaderDecodeImageData(NSData * _Nonnull imageData, NS BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly; NSNumber *scaleValue = context[SDWebImageContextImageScaleFactor]; CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); - if (scale < 1) { - scale = 1; - } - SDImageCoderOptions *coderOptions = @{SDImageCoderDecodeFirstFrameOnly : @(decodeFirstFrame), SDImageCoderDecodeScaleFactor : @(scale)}; + SDImageCoderOptions *coderOptions = @{SDImageCoderDecodeFirstFrameOnly : @(decodeFirstFrame), SDImageCoderDecodeScaleFactor : @(MAX(scale, 1))}; if (context) { SDImageCoderMutableOptions *mutableCoderOptions = [coderOptions mutableCopy]; [mutableCoderOptions setValue:context forKey:SDImageCoderWebImageContext]; @@ -93,10 +90,7 @@ UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NSData * _Nonnull im BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly; NSNumber *scaleValue = context[SDWebImageContextImageScaleFactor]; CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey); - if (scale < 1) { - scale = 1; - } - SDImageCoderOptions *coderOptions = @{SDImageCoderDecodeFirstFrameOnly : @(decodeFirstFrame), SDImageCoderDecodeScaleFactor : @(scale)}; + SDImageCoderOptions *coderOptions = @{SDImageCoderDecodeFirstFrameOnly : @(decodeFirstFrame), SDImageCoderDecodeScaleFactor : @(MAX(scale, 1))}; if (context) { SDImageCoderMutableOptions *mutableCoderOptions = [coderOptions mutableCopy]; [mutableCoderOptions setValue:context forKey:SDImageCoderWebImageContext]; diff --git a/SDWebImage/UIImage+MultiFormat.m b/SDWebImage/UIImage+MultiFormat.m index c70a3808..04d08c54 100644 --- a/SDWebImage/UIImage+MultiFormat.m +++ b/SDWebImage/UIImage+MultiFormat.m @@ -23,10 +23,7 @@ if (!data) { return nil; } - if (scale < 1) { - scale = 1; - } - SDImageCoderOptions *options = @{SDImageCoderDecodeScaleFactor : @(scale), SDImageCoderDecodeFirstFrameOnly : @(firstFrameOnly)}; + SDImageCoderOptions *options = @{SDImageCoderDecodeScaleFactor : @(MAX(scale, 1)), SDImageCoderDecodeFirstFrameOnly : @(firstFrameOnly)}; return [[SDImageCodersManager sharedManager] decodedImageWithData:data options:options]; }