从相机拍摄的iOS比例图像

时间:2013-10-31 00:22:09

标签: ios uiimage orientation scaling uiimageorientation

我正在尝试编写用户可以从相机拍摄照片的代码,然后它将显示在指定的UIImageView内,并上传到服务器以供以后用户使用。我正在使用iPad作为设备。但是,当用户拍摄照片时,它会旋转90度。此外,它缩放错误。纵横比与照片拍摄的不同。这是我用来缩放和调整大小的代码:http://pastebin.com/HxNkb7Be

将文件上传到我的服务器时,我得到Image的数据如下:

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

这是我从相机获取UIImage的方式:

// 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];

1 个答案:

答案 0 :(得分:4)

可能是因为您使用的辅助方法 [rep fullResolutionImage] 。以下是一个很好的方法,您可以使用它来实现您想要的。 (这也将解决缩放问题)

 - (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;
 }