UIImagePickerController - 将视频预览转换为横向

时间:2014-07-25 08:21:50

标签: ios iphone camera orientation uiimagepickercontroller

我目前正在开发一个录制和编辑视频的应用程序。孔应用程序仅支持横向模式 - 所以我想在横向中使用相机控件。但预览屏幕总是切换回肖像。之前曾问过类似的问题(camera preview is portrait in landscape app),但遗憾的是没有任何公认的答案。

我尝试了几种方法:

首先我手动添加了UIImagePickerController的视图 - 预览完全符合我的要求,但视频图像(录制时)是错误的(转向侧面)。

代码:

- (BOOL) startCameraController {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) {
        return NO;
    }

    self.cameraUI = [[UIImagePickerController alloc] init];
    self.cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
    self.cameraUI.allowsEditing = NO;
    self.cameraUI.delegate = self.parentViewController;

    [self.parentViewController addChildViewController:self.cameraUI];
    self.cameraUI.view.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self.parentViewController.view addSubview:self.cameraUI.view];

    return YES;
}

结果:

The preview is displayed in the correct orientation, but the camera image is turned sideways.

然后我使用presentViewController来显示UIImagePickerController - 相机录制效果非常好,但预览屏幕是纵向的,我必须让我的设备使用它。

代码:

- (BOOL) startCameraController {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) {
        return NO;
    }

    self.cameraUI = [[UIImagePickerController alloc] init];
    self.cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
    self.cameraUI.allowsEditing = NO;
    self.cameraUI.delegate = self.parentViewController;

    [self.parentViewController presentViewController:self.cameraUI animated:YES completion:^{}];

    return YES;
}

结果:

The preview screen is turned upside down, so that I have to turn my device to use the controls.

我甚至会听到事件_UIImagePickerControllerUserDidCaptureItem来检测相机何时更改为预览并尝试手动转动视图 - 但我无法调整预览视图的框架尺寸。

代码:

- (void) cameraChangedToPreview {
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];

    if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight) {
        self.cameraUI.view.transform = CGAffineTransformMakeRotation(M_PI_2);
    } else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft) {
        self.cameraUI.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
    }

    if (IS_WIDESCREEN) {
        self.cameraUI.view.frame = CGRectMake(0, 0, 568, 320);
    } else {
        self.cameraUI.view.frame = CGRectMake(0, 0, 480, 320);
    }
}

结果: The screen dimensions of the preview are wrong.

我真的很感激任何提示或建议我如何在横向模式下使用相机和预览,以便我可以使用我的孔应用程序而无需在此过程中转动设备(该设备将在应用程序中使用三脚架使用,因此无法转动。)

谢谢你,祝福!

2 个答案:

答案 0 :(得分:0)

UIImage选择器视图不能仅在LandScape模式下使用这里是苹果所说的

mportant:UIImagePickerController类仅支持纵向模式。此类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改,但有一个例外。您可以将自定义视图分配给cameraOverlayView属性,并使用该视图显示其他信息或管理相机界面与代码之间的交互。

你可以从这里了解更多相关信息 https://developer.apple.com/library/ios/documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html#//apple_ref/doc/uid/TP40007070-CH3-DontLinkElementID_1

答案 1 :(得分:0)

您可以将视图控制器显示为模态,将旋转锁定为纵向,然后聆听运动传感器并从那里检测旋转。像这样:

self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.accelerometerUpdateInterval = 0.5f;
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
                                         withHandler:^(CMAccelerometerData  *accelerometerData, NSError *error) {
                                             float x = -accelerometerData.acceleration.x;
                                             float y = accelerometerData.acceleration.y;
                                             float angle = atan2(y, x);

                                             if(angle >= -2.25 && angle <= -0.75) {
                                                 currentOrientation = UIInterfaceOrientationPortrait;
                                             } else if(angle >= -0.75 && angle <= 0.75){
                                                 currentOrientation = UIInterfaceOrientationLandscapeRight;
                                             } else if(angle >= 0.75 && angle <= 2.25) {
                                                 currentOrientation = UIInterfaceOrientationPortraitUpsideDown;
                                             } else if(angle <= -2.25 || angle >= 2.25) {
                                                 currentOrientation = UIInterfaceOrientationLandscapeLeft;
                                             }
                                         }];

之后,您可以使用currentOrientation正确旋转图像,然后保存:

[videoConnection setVideoOrientation:currentOrientation];

希望这有助于你