@@ -418,8 +418,8 @@ + (UIImage *)decodedImageWithImage:(UIImage *)image {
418
418
}
419
419
420
420
UIImage *decodedImage;
421
- #if SD_UIKIT
422
421
SDImageCoderDecodeSolution decodeSolution = self.defaultDecodeSolution ;
422
+ #if SD_UIKIT
423
423
if (decodeSolution == SDImageCoderDecodeSolutionAutomatic) {
424
424
// See #3365, CMPhoto iOS 15 only supports JPEG/HEIF format, or it will print an error log :(
425
425
SDImageFormat format = image.sd_imageFormat ;
@@ -439,19 +439,31 @@ + (UIImage *)decodedImageWithImage:(UIImage *)image {
439
439
440
440
CGImageRef imageRef = image.CGImage ;
441
441
if (!imageRef) {
442
+ // Only decode for CGImage-based
442
443
return image;
443
444
}
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
+ }
455
467
SDImageCopyAssociatedObject (image, decodedImage);
456
468
decodedImage.sd_isDecoded = YES ;
457
469
return decodedImage;
@@ -472,6 +484,10 @@ + (UIImage *)decodedAndScaledDownImageWithImage:(UIImage *)image limitBytes:(NSU
472
484
tileTotalPixels = destTotalPixels / 3 ;
473
485
474
486
CGImageRef sourceImageRef = image.CGImage ;
487
+ if (!sourceImageRef) {
488
+ // Only decode for CGImage-based
489
+ return image;
490
+ }
475
491
CGSize sourceResolution = CGSizeZero;
476
492
sourceResolution.width = CGImageGetWidth (sourceImageRef);
477
493
sourceResolution.height = CGImageGetHeight (sourceImageRef);
0 commit comments