From f84678a44d506d8518bf5e5c35185581ee453f97 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 20 Apr 2020 12:22:09 +0800 Subject: [PATCH] Add the support for embed thumbnail for JPEG/HEIF/AVIF --- SDWebImage/Core/SDImageCoder.h | 8 ++++++++ SDWebImage/Core/SDImageCoder.m | 1 + SDWebImage/Core/SDImageIOAnimatedCoder.m | 5 +++++ SDWebImage/Core/SDImageIOCoder.m | 5 +++++ 4 files changed, 19 insertions(+) diff --git a/SDWebImage/Core/SDImageCoder.h b/SDWebImage/Core/SDImageCoder.h index fe5ca0cc..fe4c5f27 100644 --- a/SDWebImage/Core/SDImageCoder.h +++ b/SDWebImage/Core/SDImageCoder.h @@ -79,6 +79,14 @@ FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeMaxPixelSi */ FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeMaxFileSize; +/** + A Boolean value indicating the encoding format should contains a thumbnail image into the output data. Only some of image format (like JPEG/HEIF/AVIF) support this behavior. The embed thumbnail will be used during next time thumbnail decoding (provided `.thumbnailPixelSize`), which is faster than full image thumbnail decoding. (NSNumber) + Defaults to NO, which does not embed any thumbnail. + @note The thumbnail image's pixel size is not defined, the encoder can choose the proper pixel size which is suitable for encoding quality. + @note works for `SDImageCoder` + */ +FOUNDATION_EXPORT SDImageCoderOption _Nonnull const SDImageCoderEncodeEmbedThumbnail; + /** 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 37dfdc41..0fda1983 100644 --- a/SDWebImage/Core/SDImageCoder.m +++ b/SDWebImage/Core/SDImageCoder.m @@ -18,5 +18,6 @@ SDImageCoderOption const SDImageCoderEncodeCompressionQuality = @"encodeCompress SDImageCoderOption const SDImageCoderEncodeBackgroundColor = @"encodeBackgroundColor"; SDImageCoderOption const SDImageCoderEncodeMaxPixelSize = @"encodeMaxPixelSize"; SDImageCoderOption const SDImageCoderEncodeMaxFileSize = @"encodeMaxFileSize"; +SDImageCoderOption const SDImageCoderEncodeEmbedThumbnail = @"encodeEmbedThumbnail"; SDImageCoderOption const SDImageCoderWebImageContext = @"webImageContext"; diff --git a/SDWebImage/Core/SDImageIOAnimatedCoder.m b/SDWebImage/Core/SDImageIOAnimatedCoder.m index 5fb28a9c..df6415c3 100644 --- a/SDWebImage/Core/SDImageIOAnimatedCoder.m +++ b/SDWebImage/Core/SDImageIOAnimatedCoder.m @@ -484,6 +484,11 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination // Remove the quality if we have file size limit properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = nil; } + BOOL embedThumbnail = NO; + if (options[SDImageCoderEncodeEmbedThumbnail]) { + embedThumbnail = [options[SDImageCoderEncodeEmbedThumbnail] boolValue]; + } + properties[(__bridge NSString *)kCGImageDestinationEmbedThumbnail] = @(embedThumbnail); BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue]; if (encodeFirstFrame || frames.count == 0) { diff --git a/SDWebImage/Core/SDImageIOCoder.m b/SDWebImage/Core/SDImageIOCoder.m index df93e14e..11565e12 100644 --- a/SDWebImage/Core/SDImageIOCoder.m +++ b/SDWebImage/Core/SDImageIOCoder.m @@ -294,6 +294,11 @@ static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestination // Remove the quality if we have file size limit properties[(__bridge NSString *)kCGImageDestinationLossyCompressionQuality] = nil; } + BOOL embedThumbnail = NO; + if (options[SDImageCoderEncodeEmbedThumbnail]) { + embedThumbnail = [options[SDImageCoderEncodeEmbedThumbnail] boolValue]; + } + properties[(__bridge NSString *)kCGImageDestinationEmbedThumbnail] = @(embedThumbnail); // Add your image to the destination. CGImageDestinationAddImage(imageDestination, imageRef, (__bridge CFDictionaryRef)properties);