Merge pull request #3357 from dreampiggy/bugfix_encode_max_pixel_size

Fix the encodeMaxPixelSize logic
This commit is contained in:
DreamPiggy 2022-06-20 21:01:07 +08:00 committed by GitHub
commit 19b4b47c60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 4 deletions

View File

@ -499,7 +499,9 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination
CGFloat pixelWidth = (CGFloat)CGImageGetWidth(imageRef); CGFloat pixelWidth = (CGFloat)CGImageGetWidth(imageRef);
CGFloat pixelHeight = (CGFloat)CGImageGetHeight(imageRef); CGFloat pixelHeight = (CGFloat)CGImageGetHeight(imageRef);
CGFloat finalPixelSize = 0; CGFloat finalPixelSize = 0;
if (maxPixelSize.width > 0 && maxPixelSize.height > 0 && pixelWidth > maxPixelSize.width && pixelHeight > maxPixelSize.height) { BOOL encodeFullImage = maxPixelSize.width == 0 || maxPixelSize.height == 0 || pixelWidth == 0 || pixelHeight == 0 || (pixelWidth <= maxPixelSize.width && pixelHeight <= maxPixelSize.height);
if (!encodeFullImage) {
// Thumbnail Encoding
CGFloat pixelRatio = pixelWidth / pixelHeight; CGFloat pixelRatio = pixelWidth / pixelHeight;
CGFloat maxPixelSizeRatio = maxPixelSize.width / maxPixelSize.height; CGFloat maxPixelSizeRatio = maxPixelSize.width / maxPixelSize.height;
if (pixelRatio > maxPixelSizeRatio) { if (pixelRatio > maxPixelSizeRatio) {

View File

@ -252,10 +252,12 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination
} }
CGFloat pixelWidth = (CGFloat)CGImageGetWidth(imageRef); CGFloat pixelWidth = (CGFloat)CGImageGetWidth(imageRef);
CGFloat pixelHeight = (CGFloat)CGImageGetHeight(imageRef); CGFloat pixelHeight = (CGFloat)CGImageGetHeight(imageRef);
if (maxPixelSize.width > 0 && maxPixelSize.height > 0 && pixelWidth > maxPixelSize.width && pixelHeight > maxPixelSize.height) { CGFloat finalPixelSize = 0;
BOOL encodeFullImage = maxPixelSize.width == 0 || maxPixelSize.height == 0 || pixelWidth == 0 || pixelHeight == 0 || (pixelWidth <= maxPixelSize.width && pixelHeight <= maxPixelSize.height);
if (!encodeFullImage) {
// Thumbnail Encoding
CGFloat pixelRatio = pixelWidth / pixelHeight; CGFloat pixelRatio = pixelWidth / pixelHeight;
CGFloat maxPixelSizeRatio = maxPixelSize.width / maxPixelSize.height; CGFloat maxPixelSizeRatio = maxPixelSize.width / maxPixelSize.height;
CGFloat finalPixelSize;
if (pixelRatio > maxPixelSizeRatio) { if (pixelRatio > maxPixelSizeRatio) {
finalPixelSize = MAX(maxPixelSize.width, maxPixelSize.width / pixelRatio); finalPixelSize = MAX(maxPixelSize.width, maxPixelSize.width / pixelRatio);
} else { } else {

View File

@ -341,7 +341,7 @@
} }
} }
- (void)test22ThatThumbnailPreserveAspectRatio { - (void)test22ThatThumbnailDecodeCalculation {
NSString *testImagePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestImageLarge" ofType:@"jpg"]; NSString *testImagePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestImageLarge" ofType:@"jpg"];
NSData *testImageData = [NSData dataWithContentsOfFile:testImagePath]; NSData *testImageData = [NSData dataWithContentsOfFile:testImagePath];
CGSize thumbnailSize = CGSizeMake(400, 300); CGSize thumbnailSize = CGSizeMake(400, 300);
@ -353,6 +353,19 @@
expect(imageSize.height).equal(263); expect(imageSize.height).equal(263);
} }
- (void)test23ThatThumbnailEncodeCalculation {
NSString *testImagePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestImageLarge" ofType:@"jpg"];
NSData *testImageData = [NSData dataWithContentsOfFile:testImagePath];
UIImage *image = [SDImageIOCoder.sharedCoder decodedImageWithData:testImageData options:nil];
expect(image.size).equal(CGSizeMake(5250, 3450));
CGSize thumbnailSize = CGSizeMake(4000, 4000); // 3450 < 4000 < 5250
NSData *encodedData = [SDImageIOCoder.sharedCoder encodedDataWithImage:image format:SDImageFormatJPEG options:@{
SDImageCoderEncodeMaxPixelSize: @(thumbnailSize)
}];
UIImage *encodedImage = [UIImage sd_imageWithData:encodedData];
expect(encodedImage.size).equal(CGSizeMake(4000, 2629));
}
#pragma mark - Utils #pragma mark - Utils
- (void)verifyCoder:(id<SDImageCoder>)coder - (void)verifyCoder:(id<SDImageCoder>)coder