Question

I am attempting to write code where a user can take a picture from their camera and it will then display inside a specified UIImageView as well as upload to a server for later user. I am using a iPad as the device. However when the user takes the picture, it is rotated 90 degrees. Also, it is scaled wrong. The aspect ratio is different then what the photo was taken. This is the code I am using to scale and size: http://pastebin.com/HxNkb7Be

When uploading the file to the my server, I get the Image's data like so:

NSData *imageData = UIImageJPEGRepresentation(scaleAndRotateImage(image), 0.90f);

This is how I get the UIImage from the camera:

// Get the asset representation
ALAssetRepresentation *rep = [asset defaultRepresentation];

// Get the right orientation
UIImageOrientation orientation = UIImageOrientationUp;
NSNumber *orientationValue = [[rep metadata] objectForKey:@"Orientation"];
if (orientationValue != nil) {
    orientation = [orientationValue intValue];
}

// Get the CG image reference and convert to UIImage
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage] scale:rep.scale orientation:orientation];
Was it helpful?

Solution

May be because of the helper method [rep fullResolutionImage] you use. The following is a good method which you can use to achieve what you want. (this will solve the scaling problem as well)

 - (UIImage *)image:(UIImage *)image scaledCopyOfSize:(CGSize)newSize {

 UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
 UIImageOrientation imageOrientation = UIImageOrientationLeft;

 switch (deviceOrientation)
 {
 case UIDeviceOrientationPortrait:
 imageOrientation = UIImageOrientationRight;
 NSLog(@"UIImageOrientationRight");
 break;
 case UIDeviceOrientationPortraitUpsideDown:
 imageOrientation = UIImageOrientationLeft;
 NSLog(@"UIImageOrientationLeft");
 break;
 case UIDeviceOrientationLandscapeLeft:
 imageOrientation = UIImageOrientationUp;
 NSLog(@"UIImageOrientationUp");
 break;
 case UIDeviceOrientationLandscapeRight:
 imageOrientation = UIImageOrientationDown;
 NSLog(@"UIImageOrientationDown");
 break;
 default:
 NSLog(@"Default");
 imageOrientation = UIImageOrientationRight ;
 break;
 }

 CGImageRef imgRef = image.CGImage;

 CGFloat width = CGImageGetWidth(imgRef);
 CGFloat height = CGImageGetHeight(imgRef);

 CGAffineTransform transform = CGAffineTransformIdentity;
 CGRect bounds = CGRectMake(0, 0, width, height);
 if (width > newSize.width || height > newSize.height) {
 CGFloat ratio = width/height;
 if (ratio > 1) {
 bounds.size.width = newSize.width;
 bounds.size.height = bounds.size.width / ratio;
 }
 else {
 bounds.size.height = newSize.height;
 bounds.size.width = bounds.size.height * ratio;
 }
 }

 CGFloat scaleRatio = bounds.size.width / width;
 CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
 CGFloat boundHeight;
 UIImageOrientation orient = imageOrientation;
 switch(orient) {

 case UIImageOrientationUp: //EXIF = 1
 transform = CGAffineTransformIdentity;
 break;

 case UIImageOrientationUpMirrored: //EXIF = 2
 transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
 transform = CGAffineTransformScale(transform, -1.0, 1.0);
 break;

 case UIImageOrientationDown: //EXIF = 3
 transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
 transform = CGAffineTransformRotate(transform, M_PI);
 break;

 case UIImageOrientationDownMirrored: //EXIF = 4
 transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
 transform = CGAffineTransformScale(transform, 1.0, -1.0);
 break;

 case UIImageOrientationLeftMirrored: //EXIF = 5
 boundHeight = bounds.size.height;
 bounds.size.height = bounds.size.width;
 bounds.size.width = boundHeight;
 transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
 transform = CGAffineTransformScale(transform, -1.0, 1.0);
 transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
 break;

 case UIImageOrientationLeft: //EXIF = 6
 boundHeight = bounds.size.height;
 bounds.size.height = bounds.size.width;
 bounds.size.width = boundHeight;
 transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
 transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
 break;

 case UIImageOrientationRightMirrored: //EXIF = 7
 boundHeight = bounds.size.height;
 bounds.size.height = bounds.size.width;
 bounds.size.width = boundHeight;
 transform = CGAffineTransformMakeScale(-1.0, 1.0);
 transform = CGAffineTransformRotate(transform, M_PI / 2.0);
 break;

 case UIImageOrientationRight: //EXIF = 8
 boundHeight = bounds.size.height;
 bounds.size.height = bounds.size.width;
 bounds.size.width = boundHeight;
 transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
 transform = CGAffineTransformRotate(transform, M_PI / 2.0);
 break;

 default:
 [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];

 }

 UIGraphicsBeginImageContext(bounds.size);

 CGContextRef context = UIGraphicsGetCurrentContext();

 if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
 CGContextScaleCTM(context, -scaleRatio, scaleRatio);
 CGContextTranslateCTM(context, -height, 0);
 }
 else {
 CGContextScaleCTM(context, scaleRatio, -scaleRatio);
 CGContextTranslateCTM(context, 0, -height);
 }

 CGContextConcatCTM(context, transform);

 CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
 UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

 return imageCopy;
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top