From f90373ce14341da40c5bd730bd5c241d1d2e8405 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sun, 12 Jun 2022 16:32:33 +0800 Subject: [PATCH 1/2] Fix the thumbnail pixel size calculation when preserveAspectRatio is true --- SDWebImage/Core/SDImageIOAnimatedCoder.m | 8 ++++---- SDWebImage/Core/SDImageIOCoder.m | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/SDWebImage/Core/SDImageIOAnimatedCoder.m b/SDWebImage/Core/SDImageIOAnimatedCoder.m index a7e08c19..b1bd1b24 100644 --- a/SDWebImage/Core/SDImageIOAnimatedCoder.m +++ b/SDWebImage/Core/SDImageIOAnimatedCoder.m @@ -238,9 +238,9 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination CGFloat pixelRatio = pixelWidth / pixelHeight; CGFloat thumbnailRatio = thumbnailSize.width / thumbnailSize.height; if (pixelRatio > thumbnailRatio) { - maxPixelSize = thumbnailSize.width; + maxPixelSize = MAX(thumbnailSize.width, thumbnailSize.width / pixelRatio); } else { - maxPixelSize = thumbnailSize.height; + maxPixelSize = MAX(thumbnailSize.height, thumbnailSize.height * pixelRatio); } } else { maxPixelSize = MAX(thumbnailSize.width, thumbnailSize.height); @@ -503,9 +503,9 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination CGFloat pixelRatio = pixelWidth / pixelHeight; CGFloat maxPixelSizeRatio = maxPixelSize.width / maxPixelSize.height; if (pixelRatio > maxPixelSizeRatio) { - finalPixelSize = maxPixelSize.width; + finalPixelSize = MAX(maxPixelSize.width, maxPixelSize.width / pixelRatio); } else { - finalPixelSize = maxPixelSize.height; + finalPixelSize = MAX(maxPixelSize.height, maxPixelSize.height * pixelRatio); } properties[(__bridge NSString *)kCGImageDestinationImageMaxPixelSize] = @(finalPixelSize); } diff --git a/SDWebImage/Core/SDImageIOCoder.m b/SDWebImage/Core/SDImageIOCoder.m index 8e1199f4..7149b444 100644 --- a/SDWebImage/Core/SDImageIOCoder.m +++ b/SDWebImage/Core/SDImageIOCoder.m @@ -257,9 +257,9 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination CGFloat maxPixelSizeRatio = maxPixelSize.width / maxPixelSize.height; CGFloat finalPixelSize; if (pixelRatio > maxPixelSizeRatio) { - finalPixelSize = maxPixelSize.width; + finalPixelSize = MAX(maxPixelSize.width, maxPixelSize.width / pixelRatio); } else { - finalPixelSize = maxPixelSize.height; + finalPixelSize = MAX(maxPixelSize.height, maxPixelSize.height * pixelRatio); } properties[(__bridge NSString *)kCGImageDestinationImageMaxPixelSize] = @(finalPixelSize); } From 3b0b2326165cc2e4b34f8c301b58a99d96dc1df6 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sun, 12 Jun 2022 16:44:21 +0800 Subject: [PATCH 2/2] Added test case test22ThatThumbnailPreserveAspectRatio --- Tests/Tests/SDImageCoderTests.m | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Tests/Tests/SDImageCoderTests.m b/Tests/Tests/SDImageCoderTests.m index 199c92be..ba927abc 100644 --- a/Tests/Tests/SDImageCoderTests.m +++ b/Tests/Tests/SDImageCoderTests.m @@ -341,6 +341,18 @@ } } +- (void)test22ThatThumbnailPreserveAspectRatio { + NSString *testImagePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestImageLarge" ofType:@"jpg"]; + NSData *testImageData = [NSData dataWithContentsOfFile:testImagePath]; + CGSize thumbnailSize = CGSizeMake(400, 300); + UIImage *image = [SDImageIOCoder.sharedCoder decodedImageWithData:testImageData options:@{ + SDImageCoderDecodePreserveAspectRatio: @(YES), + SDImageCoderDecodeThumbnailPixelSize: @(thumbnailSize)}]; + CGSize imageSize = image.size; + expect(imageSize.width).equal(400); + expect(imageSize.height).equal(263); +} + #pragma mark - Utils - (void)verifyCoder:(id)coder