Merge pull request #3468 from dreampiggy/feat/decode_solution_change

Update the decode solution to allows CoreGraphics avoid using any UIKit method
This commit is contained in:
DreamPiggy 2023-01-17 16:20:07 +08:00 committed by GitHub
commit a01715e196
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 12 deletions

View File

@ -418,8 +418,8 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
}
UIImage *decodedImage;
#if SD_UIKIT
SDImageCoderDecodeSolution decodeSolution = self.defaultDecodeSolution;
#if SD_UIKIT
if (decodeSolution == SDImageCoderDecodeSolutionAutomatic) {
// See #3365, CMPhoto iOS 15 only supports JPEG/HEIF format, or it will print an error log :(
SDImageFormat format = image.sd_imageFormat;
@ -439,19 +439,31 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
CGImageRef imageRef = image.CGImage;
if (!imageRef) {
// Only decode for CGImage-based
return image;
}
BOOL hasAlpha = [self CGImageContainsAlpha:imageRef];
// Prefer to use new Image Renderer to re-draw image, instead of low-level CGBitmapContext and CGContextDrawImage
// This can keep both OS compatible and don't fight with Apple's performance optimization
SDGraphicsImageRendererFormat *format = [[SDGraphicsImageRendererFormat alloc] init];
format.opaque = !hasAlpha;
format.scale = image.scale;
CGSize imageSize = image.size;
SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc] initWithSize:imageSize format:format];
decodedImage = [renderer imageWithActions:^(CGContextRef _Nonnull context) {
[image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
}];
if (decodeSolution == SDImageCoderDecodeSolutionCoreGraphics) {
CGImageRef decodedImageRef = [self CGImageCreateDecoded:imageRef];
#if SD_MAC
decodedImage = [[UIImage alloc] initWithCGImage:decodedImageRef scale:image.scale orientation:kCGImagePropertyOrientationUp];
#else
decodedImage = [[UIImage alloc] initWithCGImage:decodedImageRef scale:image.scale orientation:image.imageOrientation];
#endif
CGImageRelease(decodedImageRef);
} else {
BOOL hasAlpha = [self CGImageContainsAlpha:imageRef];
// Prefer to use new Image Renderer to re-draw image, instead of low-level CGBitmapContext and CGContextDrawImage
// This can keep both OS compatible and don't fight with Apple's performance optimization
SDGraphicsImageRendererFormat *format = SDGraphicsImageRendererFormat.preferredFormat;
format.opaque = !hasAlpha;
format.scale = image.scale;
CGSize imageSize = image.size;
SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc] initWithSize:imageSize format:format];
decodedImage = [renderer imageWithActions:^(CGContextRef _Nonnull context) {
[image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
}];
}
SDImageCopyAssociatedObject(image, decodedImage);
decodedImage.sd_isDecoded = YES;
return decodedImage;
@ -472,6 +484,10 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
tileTotalPixels = destTotalPixels / 3;
CGImageRef sourceImageRef = image.CGImage;
if (!sourceImageRef) {
// Only decode for CGImage-based
return image;
}
CGSize sourceResolution = CGSizeZero;
sourceResolution.width = CGImageGetWidth(sourceImageRef);
sourceResolution.height = CGImageGetHeight(sourceImageRef);