10

我正在使用 Ios6 中的 iPad 应用程序,当我们单击右栏按钮时,我正在执行如下操作:

-(IBAction)camerabuttonAction:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.delegate = self;

   self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
   [self.popoverController presentPopoverFromRect:CGRectMake(50, -250, 500, 300) inView:appDelegate.splitview.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}

我的问题是,当我处于景观模式时,如果我单击按钮。每次使用后相机以纵向模式显示(图像以反向模式显示)。但是,如果我摇动 iPad,它会在 LandScape 中显示,即方向正确。

看下面的图片

当我处于风景模式时,如果我单击按钮,相机会显示如下图像:

在此处输入图像描述

如果我摇晃 iPad,相机会显示如下图像:

在此处输入图像描述

我已经尝试了很多并用谷歌搜索,但我没有找到任何解决方案。它正在消磨我的时间,所以如果有人对此进行了研究,请指导我并发布示例代码。

4

6 回答 6

10

我在我的项目中遇到了同样的问题,我尝试了以下方法并为我工作

我的代码:

- (UIImage *)scaleAndRotateImage:(UIImage *)image {
    int kMaxResolution = 640; // Or whatever

    CGImageRef imgRef = image.CGImage;

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


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

    CGFloat scaleRatio = bounds.size.width / width;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
    UIImageOrientation orient = image.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;
}
于 2013-03-29T11:03:50.950 回答
9

您可以尝试对您的 imagePickerController 进行转换

imagePickerController.view.transform = CGAffineTransformMakeRotation(-M_PI/2);
于 2013-03-26T11:00:11.850 回答
2

我在 Google 搜索中研究了您的问题,并得到了一些结果或可能性,例如以下几点:-

结果 1

一些答案说它是一个 iOS 6 错误,所以你不能像下面的问题一样修复它:-

iPad相机弹出框预览错误的旋转和缩放

结果 2

我不认为我们可以控制相机的方向。相机方向属性是内置的,它会随着设备的方向而变化。

iPad相机方向人像模式?

结果 3

您可以在中心管理缩放的实时 iPhone 相机视图,物理移动选择器框架。通过波纹管代码:-

[picker.view setFrame:CGRectMake(xOffset,yOffset,picker.view.frame.size.width,picker.view.frame.size.height)];

希望你得到真正的解决方案。关于这个错误或问题

于 2013-03-23T07:56:34.820 回答
1

我将在这里尝试一个很好的猜测,因为我自己还没有这样做......

您是否尝试过创建 UIImagePickerController 的子类并从 ViewController(它是超类)实现 interfaceOrientation 的方法?

于 2013-03-26T10:50:21.623 回答
1

请试试这个

[popController presentPopoverFromRect:CGRectMake(1024, 
  self.view.bounds.origin.y + (self.view.bounds.size.height / 2), 1, 1) 
inView:self.view 
permittedArrowDirections:UIPopoverArrowDirectionRight 
animated:YES];
于 2013-03-26T11:20:44.877 回答
1

我有一个只有 ipad 应用程序的横向模式,我吓坏了,因为我阅读了所有这些帖子,但我使用这个非常简单的代码拍照,它非常适合我唯一的横向应用程序。重要的是,我不会从库中选择图像,我只是拍摄照片并将其放在其他图像上,它非常适合我。

if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeCamera])
    {

        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =
        UIImagePickerControllerSourceTypeCamera;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                  (NSString *) kUTTypeImage,
                                  nil];
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker
                                animated:YES completion:nil];
       // [imagePicker release];
        newMedia = YES;
    }

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self.popoverController dismissPopoverAnimated:true];
  //  [popoverController release];

    NSString *mediaType = [info
                           objectForKey:UIImagePickerControllerMediaType];
    [self dismissViewControllerAnimated:YES completion:nil];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = [info
                          objectForKey:UIImagePickerControllerOriginalImage];

        imageView.image = image;
        if (newMedia)
            UIImageWriteToSavedPhotosAlbum(image,
                                           self,
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);
    }
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
        // Code here to support video if enabled
    }
}
于 2013-06-18T18:48:55.063 回答