在横向中查看时,纵向拍摄的图像会失真

时间:2012-04-09 09:28:47

标签: iphone ios ipad

我创建了一个视图,其中有一个捕获按钮。当我点击该按钮时,相机屏幕正在打开。但在横向中观看时,以纵向拍摄的图像会失真。 如何在没有失真的情况下获得实际图像?

2 个答案:

答案 0 :(得分:4)

如果您使用ios 5,那么当您捕获图像并存储到照片中时,您必须手动调用此功能。

 - (UIImage *)fixOrientation : (UIImage *)img
 {
if (img.imageOrientation == UIImageOrientationUp) return img;
CGAffineTransform transform = CGAffineTransformIdentity;

switch (img.imageOrientation) {
    case UIImageOrientationDown:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, img.size.width, img.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;

    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, img.size.width, 0);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        break;

    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, 0, img.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        break;
}

switch (img.imageOrientation) {
    case UIImageOrientationUpMirrored:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, img.size.width, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;

    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, img.size.height, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;
}

CGContextRef ctx = CGBitmapContextCreate(NULL, img.size.width, img.size.height,
                                         CGImageGetBitsPerComponent(img.CGImage), 0,
                                         CGImageGetColorSpace(img.CGImage),
                                         CGImageGetBitmapInfo(img.CGImage));
CGContextConcatCTM(ctx, transform);
switch (img.imageOrientation) {
    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        // Grr...
        CGContextDrawImage(ctx, CGRectMake(0,0,img.size.height,img.size.width), img.CGImage);
        break;

    default:
        CGContextDrawImage(ctx, CGRectMake(0,0,img.size.width,img.size.height), img.CGImage);
        break;
}

CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img1 = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img1;

 }




- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
snapimage.image = [self fixOrientation:self.image];

 }
- (void)imagePickerController:(UIImagePickerController *)picker1 
    didFinishPickingImage:(UIImage *)image1
              editingInfo:(NSDictionary *)editingInfo
{

[picker1 dismissModalViewControllerAnimated:YES];
snapimage.image = [self fixOrientation:image1];
 }

答案 1 :(得分:2)

您需要以横向方向自定义图像到视图中心, 在shouldAutorotateToInterfaceOrientation方法

尝试这样:

if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || UIInterfaceOrientationLandscapeLeft)

{
     here customize the rect of your image
}