Add ImageIO nullable check in Image module (#23544)

Summary:
Add ImageIO nullable check in Image module to prevent crash.

[iOS] [Fixed] - Add ImageIO nullable check in Image module
Pull Request resolved: https://github.com/facebook/react-native/pull/23544

Differential Revision: D14146483

Pulled By: cpojer

fbshipit-source-id: 813caebea1e24d5e282b21cc50240ec1886339d5
This commit is contained in:
zhongwuzw
2019-02-19 21:16:36 -08:00
committed by Facebook Github Bot
parent be36aa89c5
commit f8276805ca

View File

@@ -330,12 +330,15 @@ NSDictionary<NSString *, id> *__nullable RCTGetImageMetadata(NSData *data)
NSData *__nullable RCTGetImageData(UIImage *image, float quality)
{
CGImageRef cgImage = image.CGImage;
if (!cgImage) {
return NULL;
}
NSMutableDictionary *properties = [[NSMutableDictionary alloc] initWithDictionary:@{
(id)kCGImagePropertyOrientation : @(CGImagePropertyOrientationFromUIImageOrientation(image.imageOrientation))
}];
CGImageDestinationRef destination;
CFMutableDataRef imageData = CFDataCreateMutable(NULL, 0);
CGImageRef cgImage = image.CGImage;
if (RCTImageHasAlpha(cgImage)) {
// get png data
destination = CGImageDestinationCreateWithData(imageData, kUTTypePNG, 1, NULL);
@@ -344,9 +347,12 @@ NSData *__nullable RCTGetImageData(UIImage *image, float quality)
destination = CGImageDestinationCreateWithData(imageData, kUTTypeJPEG, 1, NULL);
[properties setValue:@(quality) forKey:(id)kCGImageDestinationLossyCompressionQuality];
}
if (!destination) {
CFRelease(imageData);
return NULL;
}
CGImageDestinationAddImage(destination, cgImage, (__bridge CFDictionaryRef)properties);
if (!CGImageDestinationFinalize(destination))
{
if (!CGImageDestinationFinalize(destination)) {
CFRelease(imageData);
imageData = NULL;
}