From 06589dbcc4d4c1e3b6be1ada3ee035e483354d2e Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sun, 9 Jul 2023 20:00:04 +0800 Subject: [PATCH] Update the default force decode value, partial revert ff6b3b9bb59613d1acde21258d45bfe83bd44fab Now it's will mark as false for not byte-aligned CGImage --- SDWebImage/Core/UIImage+ForceDecode.h | 6 ++++++ SDWebImage/Core/UIImage+ForceDecode.m | 25 ++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/SDWebImage/Core/UIImage+ForceDecode.h b/SDWebImage/Core/UIImage+ForceDecode.h index f3687467..18d0bf57 100644 --- a/SDWebImage/Core/UIImage+ForceDecode.h +++ b/SDWebImage/Core/UIImage+ForceDecode.h @@ -15,6 +15,12 @@ /** A bool value indicating whether the image has already been decoded. This can help to avoid extra force decode. + Force decode is used for 2 cases: + -- 1. for ImageIO created image (via `CGImageCreateWithImageSource` SPI), it's lazy and we trigger the decode before rendering + -- 2. for non-ImageIO created image (via `CGImageCreate` API), we can ensure it's alignment is suitable to render on screen without copy by CoreAnimation + @note For coder plugin developer, always use the SDImageCoderHelper's `colorSpaceGetDeviceRGB`/`preferredByteAlignment`/`preferredBitmapInfo:` to create CGImage. + @note For more information why force decode, see: https://github.com/path/FastImageCache#byte-alignment + @note The defaults value is calculated based on UIImage's CGImage status. Do not assume it's always work as expected. Set the value to `true` if you don't need force decode */ @property (nonatomic, assign) BOOL sd_isDecoded; diff --git a/SDWebImage/Core/UIImage+ForceDecode.m b/SDWebImage/Core/UIImage+ForceDecode.m index 9fc72588..4e315cdb 100644 --- a/SDWebImage/Core/UIImage+ForceDecode.m +++ b/SDWebImage/Core/UIImage+ForceDecode.m @@ -20,19 +20,26 @@ } else { // Assume only CGImage based can use lazy decoding CGImageRef cgImage = self.CGImage; - if (cgImage) { - CFStringRef uttype = CGImageGetUTType(self.CGImage); - if (uttype) { - // Only ImageIO can set `com.apple.ImageIO.imageSourceTypeIdentifier` - return NO; - } else { - // Thumbnail or CGBitmapContext drawn image + if (!cgImage) { + // Assume others as non-decoded + return NO; + } + CFStringRef uttype = CGImageGetUTType(self.CGImage); + if (uttype) { + // Only ImageIO can set `com.apple.ImageIO.imageSourceTypeIdentifier` + return NO; + } else { + // Now, let's check if the CGImage is byte-aligned (not aligned will cause extra copy) + size_t bytesPerRow = CGImageGetBytesPerRow(cgImage); + if (SDByteAlign(bytesPerRow, [SDImageCoderHelper preferredByteAlignment]) == bytesPerRow) { + // byte aligned, OK return YES; + } else { + // not aligned, still need force-decode + return NO; } } } - // Assume others as non-decoded - return NO; } - (void)setSd_isDecoded:(BOOL)sd_isDecoded {