Merge pull request #3354 from dreampiggy/bugfix_thumbnail_calculate

Fix the thumbnail pixel size calculation when preserveAspectRatio is true
This commit is contained in:
DreamPiggy 2022-06-12 17:11:14 +08:00 committed by GitHub
commit eeb25d6e9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 6 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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<SDImageCoder>)coder