2

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

4

2 回答 2

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];
 }
于 2012-04-09T09:37:37.217 回答
2

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

试试这样:

if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || UIInterfaceOrientationLandscapeLeft)

{
     here customize the rect of your image
}
于 2012-04-09T09:36:11.710 回答