Add the possible nullable logic check when the force-decode/copy failed

This commit is contained in:
DreamPiggy 2022-11-08 16:19:49 +08:00
parent c81aec4ccd
commit 0d1c603d50
1 changed files with 13 additions and 9 deletions

View File

@ -269,9 +269,11 @@ static CGImageRef __nullable SDCGImageCreateCopy(CGImageRef cg_nullable image) {
} else { } else {
// `CGImageSourceCreateThumbnailAtIndex` take only pixel dimension, if not `preserveAspectRatio`, we should manual scale to the target size // `CGImageSourceCreateThumbnailAtIndex` take only pixel dimension, if not `preserveAspectRatio`, we should manual scale to the target size
CGImageRef scaledImageRef = [SDImageCoderHelper CGImageCreateScaled:imageRef size:thumbnailSize]; CGImageRef scaledImageRef = [SDImageCoderHelper CGImageCreateScaled:imageRef size:thumbnailSize];
CGImageRelease(imageRef); if (scaledImageRef) {
imageRef = scaledImageRef; CGImageRelease(imageRef);
isDecoded = YES; imageRef = scaledImageRef;
isDecoded = YES;
}
} }
} }
// Check whether output CGImage is decoded // Check whether output CGImage is decoded
@ -279,17 +281,19 @@ static CGImageRef __nullable SDCGImageCreateCopy(CGImageRef cg_nullable image) {
if (!isDecoded) { if (!isDecoded) {
// Use CoreGraphics to trigger immediately decode // Use CoreGraphics to trigger immediately decode
CGImageRef decodedImageRef = [SDImageCoderHelper CGImageCreateDecoded:imageRef]; CGImageRef decodedImageRef = [SDImageCoderHelper CGImageCreateDecoded:imageRef];
CGImageRelease(imageRef); if (decodedImageRef) {
imageRef = decodedImageRef; CGImageRelease(imageRef);
isDecoded = YES; imageRef = decodedImageRef;
isDecoded = YES;
}
} }
} else { } else {
// iOS 15+, CGImageRef now retains CGImageSourceRef internally. To workaround its thread-safe issue, we have to strip CGImageSourceRef, using Force-Decode (or have to use SPI `CGImageSetImageSource`), See: https://github.com/SDWebImage/SDWebImage/issues/3273 // iOS 15+, CGImageRef now retains CGImageSourceRef internally. To workaround its thread-safe issue, we have to strip CGImageSourceRef, using Force-Decode (or have to use SPI `CGImageSetImageSource`), See: https://github.com/SDWebImage/SDWebImage/issues/3273
if (@available(iOS 15, tvOS 15, *)) { if (@available(iOS 15, tvOS 15, *)) {
// User pass `lazyDecode == YES`, but we still have to strip the CGImageSourceRef // User pass `lazyDecode == YES`, but we still have to strip the CGImageSourceRef
if (imageRef) { // CGImageRef newImageRef = CGImageCreateCopy(imageRef); // This one does not strip the CGImageProperty
// CGImageRef newImageRef = CGImageCreateCopy(imageRef); CGImageRef newImageRef = SDCGImageCreateCopy(imageRef);
CGImageRef newImageRef = SDCGImageCreateCopy(imageRef); if (newImageRef) {
CGImageRelease(imageRef); CGImageRelease(imageRef);
imageRef = newImageRef; imageRef = newImageRef;
} }