Support to provide the background color when you encode a alpha UIImage into non-alpha format like JPEG
This commit is contained in:
parent
374b3f7981
commit
2ca731c2e8
|
@ -55,6 +55,12 @@ FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeFirstFrame
|
|||
*/
|
||||
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeCompressionQuality;
|
||||
|
||||
/**
|
||||
A UIColor(NSColor) value to used for non-alpha image encoding when the input image has alpha channel, the background color will be used to compose the alpha one. If not provide, use white color.
|
||||
@note works for `SDImageEncoder`
|
||||
*/
|
||||
FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeBackgroundColor;
|
||||
|
||||
/**
|
||||
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.
|
||||
|
|
|
@ -15,5 +15,6 @@ SDImageCoderOption const SDImageCoderDecodeThumbnailPixelSize = @"decodeThumbnai
|
|||
|
||||
SDImageCoderOption const SDImageCoderEncodeFirstFrameOnly = @"encodeFirstFrameOnly";
|
||||
SDImageCoderOption const SDImageCoderEncodeCompressionQuality = @"encodeCompressionQuality";
|
||||
SDImageCoderOption const SDImageCoderEncodeBackgroundColor = @"encodeBackgroundColor";
|
||||
|
||||
SDImageCoderOption const SDImageCoderWebImageContext = @"webImageContext";
|
||||
|
|
|
@ -426,11 +426,16 @@ static NSString * kSDCGImageSourceRasterizationDPI = @"kCGImageSourceRasterizati
|
|||
return nil;
|
||||
}
|
||||
NSMutableDictionary *properties = [NSMutableDictionary dictionary];
|
||||
// Encoding Options
|
||||
double compressionQuality = 1;
|
||||
if (options[SDImageCoderEncodeCompressionQuality]) {
|
||||
compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue];
|
||||
}
|
||||
properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = @(compressionQuality);
|
||||
CGColorRef backgroundColor = [options[SDImageCoderEncodeBackgroundColor] CGColor];
|
||||
if (backgroundColor) {
|
||||
properties[(__bridge NSString *)kCGImageDestinationBackgroundColor] = (__bridge id)(backgroundColor);
|
||||
}
|
||||
|
||||
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue];
|
||||
if (encodeFirstFrame || frames.count == 0) {
|
||||
|
|
|
@ -248,11 +248,16 @@
|
|||
CGImagePropertyOrientation exifOrientation = kCGImagePropertyOrientationUp;
|
||||
#endif
|
||||
properties[(__bridge NSString *)kCGImagePropertyOrientation] = @(exifOrientation);
|
||||
// Encoding Options
|
||||
double compressionQuality = 1;
|
||||
if (options[SDImageCoderEncodeCompressionQuality]) {
|
||||
compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue];
|
||||
}
|
||||
properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = @(compressionQuality);
|
||||
CGColorRef backgroundColor = [options[SDImageCoderEncodeBackgroundColor] CGColor];
|
||||
if (backgroundColor) {
|
||||
properties[(__bridge NSString *)kCGImageDestinationBackgroundColor] = (__bridge id)(backgroundColor);
|
||||
}
|
||||
|
||||
// Add your image to the destination.
|
||||
CGImageDestinationAddImage(imageDestination, image.CGImage, (__bridge CFDictionaryRef)properties);
|
||||
|
|
Loading…
Reference in New Issue