UIImagePickerController以横向方向录制视频 [英] UIImagePickerController record video with landscape orientation

查看:150
本文介绍了UIImagePickerController以横向方向录制视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何强制UIImagePickerController控制器仅以横向模式录制视频?

How can we force UIImagePickerController controller to record video only in landscape mode?

我知道这个类应该用于人像录制,但我需要它在景观中。

I know that this class should be used for portrait recording, but I need to have it in landscape.

找到了几个类似的问题,但没有任何解决方案适合我(iOS 6.1)。

Have found several similar questions but there is not any solution which works for me (iOS 6.1).

例如,观察设备方向对我不起作用(此答案 - https://stackoverflow.com/a/2147584 / 775779

For example observation of device orientation doesn't work for me (this answer- https://stackoverflow.com/a/2147584/775779)

如果我实现如下的UIImagePickerController类别:

If I implement UIImagePickerController category like the following:

#import "UIImagePickerController+NonRotating.h"

@implementation UIImagePickerController (NonRotating)

- (BOOL)shouldAutorotate
{

    return YES;
}

-(NSUInteger)supportedInterfaceOrientations

{
    return UIInterfaceOrientationMaskLandscape;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

@end

它几乎可以运作,但我在录制过程中看到某些控件的奇怪行为和错误的视频方向(结果视频正常)。

it almost works, but I see some strange behavior of some controls and wrong video orientation during recording (the result video is ok).

1)开始。取消和录制按钮在正确的位置,但其他控制错误。视频旋转。

1) Start. Cancel and recording button in the right positions, but other controls in wrong. And video is rotated.

2 )录音。定时器和视频都是错误的。

2) Recording. Timer and video are wrong.

3 )录制完成。都好!结果视频是正确的。

3) Recording finished. all good! and the result video is right.

您有什么想法吗?

更新
我需要在录制过程中以横向方向锁定屏幕。

UPDATE I need to lock the screen in Landscape orientation during the recording process.

推荐答案

您需要实现自定义的UIImagePickerController
为此,您需要将imagepicker的属性 showsCameraControls 设置为FALSE。

You need to implement a custom UIImagePickerController. For that, you'll need to set imagepicker's property showsCameraControls to FALSE.

self.mImagePickerController.showsCameraControls = NO;
self.mImagePickerController.wantsFullScreenLayout = YES;

执行此操作后,您将看不到默认控件。您需要设置自定义按钮以开始/停止录制和翻转相机等。

Once you do this, no default controls will be visible to you. You'll need to setup custom buttons for start/stop recording and flipping camera etc.

现在,您可以使用开始/停止方法录制视频:

Now, you can use the start/stop methods to record a video:

当点击开始录制时,

[self.mImagePickerController startVideoCapture];

要停止,

[self.mImagePickerController stopVideoCapture];

要跟踪相机之间的切换,可以使用标记

To keep track of the switching between the camera, you can use a flag

if (self.mImagePickerController.cameraDevice == UIImagePickerControllerCameraDeviceRear)
    {
        self.mImagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        isFrontCamera = YES;
    }
    else
    {
        self.mImagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        isFrontCamera = NO;
    }

您可以根据方向更改设置控件。
AppDelegate.m

You can set the controls as you wish on the orientation change. AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

这是在方向更改时调用的委托。您可以使用特定类的对象来调用其shouldAutorotate方法,并根据方向设置摄像机控制位置。

This is the delegate that gets called on orientation change. You can use the particular class's object to call its shouldAutorotate method and setup your camera control positions according to the orientation.

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    [self.objController shouldAutorotate];
    return UIInterfaceOrientationMaskAll;
}

现在在cameraviewcontroller.m里面

Now inside cameraviewcontroller.m

-(BOOL)shouldAutorotate
{
    UIInterfaceOrientation interfaceOrientation = [[UIDevice currentDevice]orientation];

    if(interfaceOrientation == UIInterfaceOrientationPortrait)
    {
        // your controls setup
    }
    else
    {
        // view accordingly for landscape 
    }
}

我试图涵盖所有点。如果您有任何困惑,请告诉我。希望它有所帮助:)

I have tried to cover all the points. Let me know if you have any confusion. Hope it helps :)

1)您需要检查翻盖相机按钮的点击事件的条件。

1) You need to check the condition on click event of the flip camera button.

2)AppDelegate.h:声明您的类的对象,您可以在其中录制视频并创建属性。这里我使用 CameraViewController 作为示例。

2) AppDelegate.h: Declare an object of your class where you record your video and create a property. Here I'm using CameraViewController as an example.

CameraViewController *objController;

现在在AppDelegate.m中:

Now in AppDelegate.m:

self.objController = [[CameraViewController alloc]initWithNibName:@"CameraViewController" bundle:nil];

现在使用此实例调用 shouldAutorotate 如上所示的方法。并返回横向:

Now use this instance to call the shouldAutorotate method as I have shown above. And return landscape orientation:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if(isOptionScreenActive == YES)
    {
        [self.shareController shouldAutorotate];
        return UIInterfaceOrientationMaskLandscape;
    }
    else
    {
        [self.anotherController shouldAutorotate];
        return UIInterfaceOrientationMaskPortrait;
    }
}

此处 isOptionScreenActive 标志,它是在录制类的 viewWillAppear 中设置的。在 viewDidUnload 中设置 false 。或者可能是另一个类的 viewWillAppear

Here isOptionScreenActive is a flag that is set in the viewWillAppear of the recording class. Set it false in viewDidUnload. Or may be viewWillAppear of another class.

3)我以cameraviewcontroller.m为例。它反映了您的录音课程。同样在您的视频录制课程的 shouldAutorotate 方法中,如果检测到的方向是肖像,则返回NO。这样UI就不会改变,你只能将UI保持在横向上。

3) I have used cameraviewcontroller.m just as an example. It reflects your recording class. Also in your video recording class's shouldAutorotate method, if the detected orientation is portrait, just return NO. This way the UI wont change and you can keep the UI in landscape only.

这篇关于UIImagePickerController以横向方向录制视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆