Added the support for max file size for lossy encoding
This commit is contained in:
parent
db610363f4
commit
b427ad5f3f
|
@ -71,6 +71,13 @@ FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeBackground
|
|||
*/
|
||||
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeMaxPixelSize;
|
||||
|
||||
/**
|
||||
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 works for `SDImageCoder`
|
||||
*/
|
||||
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeMaxFileSize;
|
||||
|
||||
/**
|
||||
A SDWebImageContext object which hold the original context options from top-level API. (SDWebImageContext)
|
||||
This option is ignored for all built-in coders and take no effect.
|
||||
|
|
|
@ -17,5 +17,6 @@ SDImageCoderOption const SDImageCoderEncodeFirstFrameOnly = @"encodeFirstFrameOn
|
|||
SDImageCoderOption const SDImageCoderEncodeCompressionQuality = @"encodeCompressionQuality";
|
||||
SDImageCoderOption const SDImageCoderEncodeBackgroundColor = @"encodeBackgroundColor";
|
||||
SDImageCoderOption const SDImageCoderEncodeMaxPixelSize = @"encodeMaxPixelSize";
|
||||
SDImageCoderOption const SDImageCoderEncodeMaxFileSize = @"encodeMaxFileSize";
|
||||
|
||||
SDImageCoderOption const SDImageCoderWebImageContext = @"webImageContext";
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
// Specify DPI for vector format in CGImageSource, like PDF
|
||||
static NSString * kSDCGImageSourceRasterizationDPI = @"kCGImageSourceRasterizationDPI";
|
||||
// Specify File Size for lossy format encoding, like JPEG
|
||||
static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestinationRequestedFileSize";
|
||||
|
||||
@interface SDImageIOCoderFrame : NSObject
|
||||
|
||||
|
@ -462,6 +464,10 @@ static NSString * kSDCGImageSourceRasterizationDPI = @"kCGImageSourceRasterizati
|
|||
finalPixelSize = maxPixelSize.height;
|
||||
}
|
||||
}
|
||||
NSUInteger maxFileSize = [options[SDImageCoderEncodeMaxFileSize] unsignedIntegerValue];
|
||||
if (maxFileSize > 0) {
|
||||
properties[kSDCGImageDestinationRequestedFileSize] = @(maxFileSize);
|
||||
}
|
||||
|
||||
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue];
|
||||
if (encodeFirstFrame || frames.count == 0) {
|
||||
|
|
|
@ -14,6 +14,9 @@
|
|||
#import "SDImageHEICCoderInternal.h"
|
||||
#import "SDImageIOAnimatedCoderInternal.h"
|
||||
|
||||
// Specify File Size for lossy format encoding, like JPEG
|
||||
static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestinationRequestedFileSize";
|
||||
|
||||
@implementation SDImageIOCoder {
|
||||
size_t _width, _height;
|
||||
CGImagePropertyOrientation _orientation;
|
||||
|
@ -285,7 +288,10 @@
|
|||
}
|
||||
properties[(__bridge NSString *)kCGImageDestinationImageMaxPixelSize] = @(finalPixelSize);
|
||||
}
|
||||
|
||||
NSUInteger maxFileSize = [options[SDImageCoderEncodeMaxFileSize] unsignedIntegerValue];
|
||||
if (maxFileSize > 0) {
|
||||
properties[kSDCGImageDestinationRequestedFileSize] = @(maxFileSize);
|
||||
}
|
||||
|
||||
// Add your image to the destination.
|
||||
CGImageDestinationAddImage(imageDestination, imageRef, (__bridge CFDictionaryRef)properties);
|
||||
|
|
Loading…
Reference in New Issue