cordova/cordova-ios — Issue #9: ios camera targetWidth/Height don't match the documentation
Author: Vickor
Created: 2012-01-19 02:15:57+00:00
State: open

According to the phonegap camera documentation using targetWidth and targetHeight should maintain the aspect ratio of the original photo ( http://docs.phonegap.com/en/1.3.0/phonegap_camera_camera.md.html#cameraOptions ) . The ios implementation does not do this. Instead it makes the shortest side match the desired target, and crops the rest.

For example, if I take a 1920x1200 picture, and say targetWidth=1024 and targetHeight=1024, it results in a 1024x1024 square picture with the excess removed. On android it results in something like 1024x640, and nothing is cropped.

I'm don't know objective-c, but I believe the fix is to use the following code inside Camera.m imageByScalingAndCroppingForSize. This is based off the android implementation.

...
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat newRatio = targetWidth / targetHeight;
CGFloat origRatio = width / height;

if (origRatio > newRatio)
{
targetHeight = (targetWidth * height) / width
}
else
{
targetWidth = (targetHeight * width) / height;
}
}

CGSize finalSize = CGSizeMake(targetWidth, targetHeight);
UIGraphicsBeginImageContext(finalSize);

CGRect thumbnailRect = CGRectZero;
thumbnailRect.size.width = targetWidth;
thumbnailRect.size.height = targetHeight;

[sourceImage drawInRect:thumbnailRect];
...