From c025df684a5a107df9357139f4419efb2293ed04 Mon Sep 17 00:00:00 2001 From: Jehan Date: Wed, 15 Jul 2015 23:56:42 -0700 Subject: [PATCH] improve memory usage of rotation --- RCTCameraManager.m | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/RCTCameraManager.m b/RCTCameraManager.m index d19a143..c5b690d 100644 --- a/RCTCameraManager.m +++ b/RCTCameraManager.m @@ -323,24 +323,29 @@ RCT_EXPORT_METHOD(stopCapture) { NSMutableDictionary *imageMetadata = [(NSDictionary *) CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, 0, NULL)) mutableCopy]; // create cgimage - CGImageRef cgImage; - cgImage = CGImageSourceCreateImageAtIndex(source, 0, NULL); + CGImageRef CGImage; + CGImage = CGImageSourceCreateImageAtIndex(source, 0, NULL); + // Rotate it + CGImageRef rotatedCGImage; if ([options objectForKey:@"rotation"]) { float rotation = [[options objectForKey:@"rotation"] floatValue]; - cgImage = [self CGImageRotatedByAngle:cgImage angle:rotation]; + rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:rotation]; } else { // Get metadata orientation int metadataOrientation = [[imageMetadata objectForKey:(NSString *)kCGImagePropertyOrientation] intValue]; if (metadataOrientation == 6) { - cgImage = [self CGImageRotatedByAngle:cgImage angle:270]; + rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:270]; } else if (metadataOrientation == 1) { - cgImage = [self CGImageRotatedByAngle:cgImage angle:0]; + rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:0]; } else if (metadataOrientation == 3) { - cgImage = [self CGImageRotatedByAngle:cgImage angle:180]; + rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:180]; + } else { + rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:0]; } } + CGImageRelease(CGImage); // Erase metadata orientation [imageMetadata removeObjectForKey:(NSString *)kCGImagePropertyOrientation]; @@ -353,11 +358,12 @@ RCT_EXPORT_METHOD(stopCapture) { // Create destination thing NSMutableData *rotatedImageData = [NSMutableData data]; CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)rotatedImageData, CGImageSourceGetType(source), 1, NULL); + CFRelease(source); // add the image to the destination, reattaching metadata - CGImageDestinationAddImage(destination, cgImage, (CFDictionaryRef) imageMetadata); + CGImageDestinationAddImage(destination, rotatedCGImage, (CFDictionaryRef) imageMetadata); // And write CGImageDestinationFinalize(destination); - + CFRelease(destination); if (rotatedImageData) { [self saveImage:rotatedImageData target:target metadata:imageMetadata callback:callback]; @@ -366,9 +372,7 @@ RCT_EXPORT_METHOD(stopCapture) { callback(@[RCTMakeError(error.description, nil, nil)]); } - CGImageRelease(cgImage); - CFRelease(source); - CFRelease(destination); + CGImageRelease(rotatedCGImage); }]; } }); @@ -407,7 +411,7 @@ RCT_EXPORT_METHOD(stopCapture) { callback(@[[NSNull null], responseString]); } -- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle +- (CGImageRef)newCGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle { CGFloat angleInRadians = angle * (M_PI / 180); CGFloat width = CGImageGetWidth(imgRef);