Fix the false decode implementation using the ImageRenderer instead of directly create CGContext

This commit is contained in:
DreamPiggy 2022-03-15 17:54:44 +08:00
parent 0228390699
commit 51d1b50ef4
1 changed files with 13 additions and 7 deletions

View File

@ -15,6 +15,7 @@
#import "SDAssociatedObject.h" #import "SDAssociatedObject.h"
#import "UIImage+Metadata.h" #import "UIImage+Metadata.h"
#import "SDInternalMacros.h" #import "SDInternalMacros.h"
#import "SDGraphicsImageRenderer.h"
#import <Accelerate/Accelerate.h> #import <Accelerate/Accelerate.h>
static inline size_t SDByteAlign(size_t size, size_t alignment) { static inline size_t SDByteAlign(size_t size, size_t alignment) {
@ -337,16 +338,21 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
return image; return image;
} }
CGImageRef imageRef = [self CGImageCreateDecoded:image.CGImage]; CGImageRef imageRef = image.CGImage;
if (!imageRef) { if (!imageRef) {
return image; return image;
} }
#if SD_MAC BOOL hasAlpha = [self CGImageContainsAlpha:imageRef];
UIImage *decodedImage = [[UIImage alloc] initWithCGImage:imageRef scale:image.scale orientation:kCGImagePropertyOrientationUp]; // Prefer to use new Image Renderer to re-draw image, instead of low-level CGBitmapContext and CGContextDrawImage
#else // This can keep both OS compatible and don't fight with Apple's performance optimization
UIImage *decodedImage = [[UIImage alloc] initWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation]; SDGraphicsImageRendererFormat *format = [[SDGraphicsImageRendererFormat alloc] init];
#endif format.opaque = !hasAlpha;
CGImageRelease(imageRef); format.scale = image.scale;
CGSize imageSize = image.size;
SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc] initWithSize:imageSize format:format];
UIImage *decodedImage = [renderer imageWithActions:^(CGContextRef _Nonnull context) {
[image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
}];
SDImageCopyAssociatedObject(image, decodedImage); SDImageCopyAssociatedObject(image, decodedImage);
decodedImage.sd_isDecoded = YES; decodedImage.sd_isDecoded = YES;
return decodedImage; return decodedImage;