improve memory usage of rotation

This commit is contained in:
Jehan 2015-07-15 23:56:42 -07:00
parent 77ef6a49d9
commit c025df684a
1 changed files with 16 additions and 12 deletions

View File

@ -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);