From 52b24f60a3dd9ec510d9fc5beb446f9d53c81e59 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Wed, 22 Jun 2022 16:41:44 +0800 Subject: [PATCH] Fix the encoding maxPixelSize logic using the new calculation from SDWebImage Core --- Cartfile | 2 +- Package.swift | 2 +- Podfile | 2 + SDWebImageWebPCoder.podspec | 2 +- .../Classes/SDImageWebPCoder.m | 46 +++---------------- 5 files changed, 12 insertions(+), 42 deletions(-) diff --git a/Cartfile b/Cartfile index 0e62be5..e3cbb9f 100644 --- a/Cartfile +++ b/Cartfile @@ -1,2 +1,2 @@ -github "SDWebImage/SDWebImage" ~> 5.10 +github "SDWebImage/SDWebImage" ~> 5.13 github "SDWebImage/libwebp-Xcode" ~> 1.0 diff --git a/Package.swift b/Package.swift index 08fbb18..f0b4d98 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,7 @@ let package = Package( dependencies: [ // Dependencies declare other packages that this package depends on. // .package(url: /* package url */, from: "1.0.0"), - .package(url: "https://github.com/SDWebImage/SDWebImage.git", from: "5.10.0"), + .package(url: "https://github.com/SDWebImage/SDWebImage.git", from: "5.13.0"), .package(url: "https://github.com/SDWebImage/libwebp-Xcode.git", from: "1.1.0") ], targets: [ diff --git a/Podfile b/Podfile index a97e6c6..30eccae 100644 --- a/Podfile +++ b/Podfile @@ -8,6 +8,7 @@ target 'SDWebImageWebPCoderExample' do platform :ios, '9.0' project example_project_path pod 'SDWebImageWebPCoder', :path => './' + pod 'SDWebImage', :path => '../SDWebImage' end target 'SDWebImageWebPCoderTests' do @@ -15,4 +16,5 @@ target 'SDWebImageWebPCoderTests' do project test_project_path pod 'Expecta' pod 'SDWebImageWebPCoder', :path => './' + pod 'SDWebImage', :path => '../SDWebImage' end diff --git a/SDWebImageWebPCoder.podspec b/SDWebImageWebPCoder.podspec index 0949afd..65fee25 100644 --- a/SDWebImageWebPCoder.podspec +++ b/SDWebImageWebPCoder.podspec @@ -27,7 +27,7 @@ This is a SDWebImage coder plugin to support WebP image. 'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) SD_WEBP=1 WEBP_USE_INTRINSICS=1', 'USER_HEADER_SEARCH_PATHS' => '$(inherited) $(SRCROOT)/libwebp/src' } - s.dependency 'SDWebImage/Core', '~> 5.10' + s.dependency 'SDWebImage/Core', '~> 5.13' s.dependency 'libwebp', '~> 1.0' end diff --git a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m index d31a830..cb4db7c 100644 --- a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m +++ b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m @@ -68,38 +68,6 @@ else OSSpinLockUnlock(&lock##_deprecated); #endif #endif -/// Calculate the actual thumnail pixel size -static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio, CGSize thumbnailSize) { - CGFloat width = fullSize.width; - CGFloat height = fullSize.height; - CGFloat resultWidth; - CGFloat resultHeight; - - if (width == 0 || height == 0 || thumbnailSize.width == 0 || thumbnailSize.height == 0 || (width <= thumbnailSize.width && height <= thumbnailSize.height)) { - // Full Pixel - resultWidth = width; - resultHeight = height; - } else { - // Thumbnail - if (preserveAspectRatio) { - CGFloat pixelRatio = width / height; - CGFloat thumbnailRatio = thumbnailSize.width / thumbnailSize.height; - if (pixelRatio > thumbnailRatio) { - resultWidth = thumbnailSize.width; - resultHeight = ceil(thumbnailSize.width / pixelRatio); - } else { - resultHeight = thumbnailSize.height; - resultWidth = ceil(thumbnailSize.height * pixelRatio); - } - } else { - resultWidth = thumbnailSize.width; - resultHeight = thumbnailSize.height; - } - } - - return CGSizeMake(resultWidth, resultHeight); -} - @interface SDWebPCoderFrame : NSObject @property (nonatomic, assign) NSUInteger index; // Frame index (zero based) @@ -231,7 +199,7 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio int canvasWidth = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_WIDTH); int canvasHeight = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_HEIGHT); // Check whether we need to use thumbnail - CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(canvasWidth, canvasHeight), preserveAspectRatio, thumbnailSize); + CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(canvasWidth, canvasHeight) scaleSize:thumbnailSize preserveAspectRatio:preserveAspectRatio shouldScaleUp:NO]; if (!hasAnimation || decodeFirstFrame) { // first frame for animated webp image @@ -437,7 +405,7 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio scale = 1; } } - CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(width, height), _preserveAspectRatio, _thumbnailSize); + CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(width, height) scaleSize:_thumbnailSize preserveAspectRatio:_preserveAspectRatio shouldScaleUp:NO]; // Check whether we need to use thumbnail if (!CGSizeEqualToSize(CGSizeMake(width, height), scaledSize)) { CGImageRef scaledImageRef = [SDImageCoderHelper CGImageCreateScaled:newImageRef size:scaledSize]; @@ -860,8 +828,8 @@ static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio } // Check if need to scale pixel size - if (maxPixelSize.width > 0 && maxPixelSize.height > 0 && (width > maxPixelSize.width || height > maxPixelSize.height)) { - CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(width, height), YES, maxPixelSize); + CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(width, height) scaleSize:maxPixelSize preserveAspectRatio:YES shouldScaleUp:NO]; + if (!CGSizeEqualToSize(scaledSize, CGSizeMake(width, height))) { result = WebPPictureRescale(&picture, scaledSize.width, scaledSize.height); if (!result) { WebPMemoryWriterClear(&writer); @@ -1132,10 +1100,10 @@ static float GetFloatValueForKey(NSDictionary * _Nonnull dictionary, NSString * } _canvas = canvas; } - CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(_canvasWidth, _canvasHeight), _preserveAspectRatio, _thumbnailSize); + CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(_canvasWidth, _canvasHeight) scaleSize:_thumbnailSize preserveAspectRatio:_preserveAspectRatio shouldScaleUp:NO]; imageRef = [self sd_drawnWebpImageWithCanvas:_canvas iterator:iter colorSpace:_colorSpace scaledSize:scaledSize]; } else { - CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(iter.width, iter.height), _preserveAspectRatio, _thumbnailSize); + CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(iter.width, iter.height) scaleSize:_thumbnailSize preserveAspectRatio:_preserveAspectRatio shouldScaleUp:NO]; imageRef = [self sd_createWebpImageWithData:iter.fragment colorSpace:_colorSpace scaledSize:scaledSize]; } if (!imageRef) { @@ -1213,7 +1181,7 @@ static float GetFloatValueForKey(NSDictionary * _Nonnull dictionary, NSString * // Now the canvas is ready, which respects of dispose method behavior. Just do normal decoding and produce image. // Check whether we need to use thumbnail - CGSize scaledSize = SDCalculateThumbnailSize(CGSizeMake(_canvasWidth, _canvasHeight), _preserveAspectRatio, _thumbnailSize); + CGSize scaledSize = [SDImageCoderHelper scaledSizeWithImageSize:CGSizeMake(_canvasWidth, _canvasHeight) scaleSize:_thumbnailSize preserveAspectRatio:_preserveAspectRatio shouldScaleUp:NO]; CGImageRef imageRef = [self sd_drawnWebpImageWithCanvas:_canvas iterator:iter colorSpace:_colorSpace scaledSize:scaledSize]; if (!imageRef) { return nil;