From 2ca731c2e8ba2e4f2237aaeb1890ca7e6a8ca766 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 3 Apr 2020 22:52:43 +0800 Subject: [PATCH] Support to provide the background color when you encode a alpha UIImage into non-alpha format like JPEG --- SDWebImage/Core/SDImageCoder.h | 6 ++++++ SDWebImage/Core/SDImageCoder.m | 1 + SDWebImage/Core/SDImageIOAnimatedCoder.m | 5 +++++ SDWebImage/Core/SDImageIOCoder.m | 5 +++++ 4 files changed, 17 insertions(+) diff --git a/SDWebImage/Core/SDImageCoder.h b/SDWebImage/Core/SDImageCoder.h index 038b41f3..3e0f3b72 100644 --- a/SDWebImage/Core/SDImageCoder.h +++ b/SDWebImage/Core/SDImageCoder.h @@ -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. diff --git a/SDWebImage/Core/SDImageCoder.m b/SDWebImage/Core/SDImageCoder.m index df5224ae..14893ff2 100644 --- a/SDWebImage/Core/SDImageCoder.m +++ b/SDWebImage/Core/SDImageCoder.m @@ -15,5 +15,6 @@ SDImageCoderOption const SDImageCoderDecodeThumbnailPixelSize = @"decodeThumbnai SDImageCoderOption const SDImageCoderEncodeFirstFrameOnly = @"encodeFirstFrameOnly"; SDImageCoderOption const SDImageCoderEncodeCompressionQuality = @"encodeCompressionQuality"; +SDImageCoderOption const SDImageCoderEncodeBackgroundColor = @"encodeBackgroundColor"; SDImageCoderOption const SDImageCoderWebImageContext = @"webImageContext"; diff --git a/SDWebImage/Core/SDImageIOAnimatedCoder.m b/SDWebImage/Core/SDImageIOAnimatedCoder.m index 1db7a495..f5d518eb 100644 --- a/SDWebImage/Core/SDImageIOAnimatedCoder.m +++ b/SDWebImage/Core/SDImageIOAnimatedCoder.m @@ -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) { diff --git a/SDWebImage/Core/SDImageIOCoder.m b/SDWebImage/Core/SDImageIOCoder.m index f617f437..5ceefc4b 100644 --- a/SDWebImage/Core/SDImageIOCoder.m +++ b/SDWebImage/Core/SDImageIOCoder.m @@ -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);