Remove the compression quality when have max file size limit, and update the test cases for JPEG

This commit is contained in:
DreamPiggy 2020-04-04 15:52:26 +08:00
parent b427ad5f3f
commit 6316f08bb8
4 changed files with 25 additions and 1 deletions

View File

@ -73,7 +73,7 @@ FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeMaxPixelSi
/**
A NSUInteger value specify the max ouput data bytes size after encoding. Some lossy format like JPEG/HEIF supports the hint for codec to automatically reduce the quality and match the file size you want. Note this option will override the `SDImageCoderEncodeCompressionQuality`, because now the quality is decided by the encoder. (NSNumber)
@note Not all format supports this feature. And this options does not works for vector images.
@note This is a hint, no gurantee for output size because of compression algorithm limit. And this options does not works for vector images.
@note works for `SDImageCoder`
*/
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeMaxFileSize;

View File

@ -467,6 +467,8 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination
NSUInteger maxFileSize = [options[SDImageCoderEncodeMaxFileSize] unsignedIntegerValue];
if (maxFileSize > 0) {
properties[kSDCGImageDestinationRequestedFileSize] = @(maxFileSize);
// Remove the quality if we have file size limit
properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = nil;
}
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue];

View File

@ -291,6 +291,8 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination
NSUInteger maxFileSize = [options[SDImageCoderEncodeMaxFileSize] unsignedIntegerValue];
if (maxFileSize > 0) {
properties[kSDCGImageDestinationRequestedFileSize] = @(maxFileSize);
// Remove the quality if we have file size limit
properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = nil;
}
// Add your image to the destination.

View File

@ -98,6 +98,26 @@
expect(testColor.sd_hexString).equal(backgroundColor.sd_hexString);
}
- (void)test09ThatJPGImageEncodeWithMaxFileSize {
NSString * testImagePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestImageLarge" ofType:@"jpg"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:testImagePath];
// This large JPEG encoding size between (770KB ~ 2.23MB)
NSUInteger limitFileSize = 1 * 1024 * 1024; // 1MB
// 100 quality (biggest)
NSData *maxEncodedData = [SDImageCodersManager.sharedManager encodedDataWithImage:image format:SDImageFormatJPEG options:nil];
expect(maxEncodedData).notTo.beNil();
expect(maxEncodedData.length).beGreaterThan(limitFileSize);
// 0 quality (smallest)
NSData *minEncodedData = [SDImageCodersManager.sharedManager encodedDataWithImage:image format:SDImageFormatJPEG options:@{SDImageCoderEncodeCompressionQuality : @(0)}];
expect(minEncodedData).notTo.beNil();
expect(minEncodedData.length).beLessThan(limitFileSize);
NSData *limitEncodedData = [SDImageCodersManager.sharedManager encodedDataWithImage:image format:SDImageFormatJPEG options:@{SDImageCoderEncodeMaxFileSize : @(limitFileSize)}];
expect(limitEncodedData).notTo.beNil();
// So, if we limit the file size, the output data should in (770KB ~ 2.23MB)
expect(limitEncodedData.length).beLessThan(maxEncodedData.length);
expect(limitEncodedData.length).beGreaterThan(minEncodedData.length);
}
- (void)test11ThatAPNGPCoderWorks {
NSURL *APNGURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageAnimated" withExtension:@"apng"];
[self verifyCoder:[SDImageAPNGCoder sharedCoder]