Skip to content

Commit c0f7023

Browse files
committed
Update the decode solution to allows CoreGraphics avoid using any UIKit method
This is back compatible to old version's RAM behavior
1 parent 05f7fb9 commit c0f7023

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

SDWebImage/Core/SDImageCoderHelper.m

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ + (UIImage *)decodedImageWithImage:(UIImage *)image {
418418
}
419419

420420
UIImage *decodedImage;
421-
#if SD_UIKIT
422421
SDImageCoderDecodeSolution decodeSolution = self.defaultDecodeSolution;
422+
#if SD_UIKIT
423423
if (decodeSolution == SDImageCoderDecodeSolutionAutomatic) {
424424
// See #3365, CMPhoto iOS 15 only supports JPEG/HEIF format, or it will print an error log :(
425425
SDImageFormat format = image.sd_imageFormat;
@@ -439,19 +439,31 @@ + (UIImage *)decodedImageWithImage:(UIImage *)image {
439439

440440
CGImageRef imageRef = image.CGImage;
441441
if (!imageRef) {
442+
// Only decode for CGImage-based
442443
return image;
443444
}
444-
BOOL hasAlpha = [self CGImageContainsAlpha:imageRef];
445-
// Prefer to use new Image Renderer to re-draw image, instead of low-level CGBitmapContext and CGContextDrawImage
446-
// This can keep both OS compatible and don't fight with Apple's performance optimization
447-
SDGraphicsImageRendererFormat *format = [[SDGraphicsImageRendererFormat alloc] init];
448-
format.opaque = !hasAlpha;
449-
format.scale = image.scale;
450-
CGSize imageSize = image.size;
451-
SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc] initWithSize:imageSize format:format];
452-
decodedImage = [renderer imageWithActions:^(CGContextRef _Nonnull context) {
453-
[image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
454-
}];
445+
446+
if (decodeSolution == SDImageCoderDecodeSolutionCoreGraphics) {
447+
CGImageRef decodedImageRef = [self CGImageCreateDecoded:imageRef];
448+
#if SD_MAC
449+
decodedImage = [[UIImage alloc] initWithCGImage:decodedImageRef scale:image.scale orientation:kCGImagePropertyOrientationUp];
450+
#else
451+
decodedImage = [[UIImage alloc] initWithCGImage:decodedImageRef scale:image.scale orientation:image.imageOrientation];
452+
#endif
453+
CGImageRelease(decodedImageRef);
454+
} else {
455+
BOOL hasAlpha = [self CGImageContainsAlpha:imageRef];
456+
// Prefer to use new Image Renderer to re-draw image, instead of low-level CGBitmapContext and CGContextDrawImage
457+
// This can keep both OS compatible and don't fight with Apple's performance optimization
458+
SDGraphicsImageRendererFormat *format = SDGraphicsImageRendererFormat.preferredFormat;
459+
format.opaque = !hasAlpha;
460+
format.scale = image.scale;
461+
CGSize imageSize = image.size;
462+
SDGraphicsImageRenderer *renderer = [[SDGraphicsImageRenderer alloc] initWithSize:imageSize format:format];
463+
decodedImage = [renderer imageWithActions:^(CGContextRef _Nonnull context) {
464+
[image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
465+
}];
466+
}
455467
SDImageCopyAssociatedObject(image, decodedImage);
456468
decodedImage.sd_isDecoded = YES;
457469
return decodedImage;
@@ -472,6 +484,10 @@ + (UIImage *)decodedAndScaledDownImageWithImage:(UIImage *)image limitBytes:(NSU
472484
tileTotalPixels = destTotalPixels / 3;
473485

474486
CGImageRef sourceImageRef = image.CGImage;
487+
if (!sourceImageRef) {
488+
// Only decode for CGImage-based
489+
return image;
490+
}
475491
CGSize sourceResolution = CGSizeZero;
476492
sourceResolution.width = CGImageGetWidth(sourceImageRef);
477493
sourceResolution.height = CGImageGetHeight(sourceImageRef);

0 commit comments

Comments
 (0)