UIImagePickerController如何记录横向方向的视频

13

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

我知道这个类应该用于纵向录制,但我需要它横向录制。

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

例如,设备方向观察对我不起作用(此答案-https://dev59.com/7XI95IYBdhLWcg3w8iv1#2147584

如果我像下面这样实现UIImagePickerController类别:

#import "UIImagePickerController+NonRotating.h"

@implementation UIImagePickerController (NonRotating)

- (BOOL)shouldAutorotate
{

    return YES;
}

-(NSUInteger)supportedInterfaceOrientations

{
    return UIInterfaceOrientationMaskLandscape;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

@end

它几乎可以工作,但我在录制过程中看到了一些控件的奇怪行为和错误的视频方向(结果视频没问题)。

1)开始。取消和录制按钮位置正确,但其他控件位置不对。并且视频被旋转了。 enter image description here

2)录制。计时器和视频是错误的。 enter image description here

3)录制完成。一切都很好!结果视频正确。

enter image description here

你有任何想法吗?

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


3
请不要关闭这个问题。你给出的链接所对应的问题没有正确答案,而那些评论中提供的解决方案都无法解决问题。 - Dmitry Khryukin
@iPatel,你看到我的问题了吗?我已经提到了这个链接。 - Dmitry Khryukin
你尝试过设置视频方向吗? - Gotschi
@Gotschi 没有,我没有尝试过。我不使用AVCaptureConnection,我使用UIImagePickerController。 - Dmitry Khryukin
你必须以某种方式旋转设备.... :( - AsifHabib
2个回答

3
你需要实现一个自定义的UIImagePickerController。为此,你需要将图片选择器的showsCameraControls属性设置为FALSE。
self.mImagePickerController.showsCameraControls = NO;
self.mImagePickerController.wantsFullScreenLayout = YES;

一旦您这样做,将不会显示任何默认控件。您需要设置自定义按钮来开始/停止录制和翻转相机等。

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

当单击“开始录制”时,

[self.mImagePickerController startVideoCapture];

停止:

[self.mImagePickerController stopVideoCapture];

为了追踪相机之间的切换,你可以使用一个标记。
if (self.mImagePickerController.cameraDevice == UIImagePickerControllerCameraDeviceRear)
    {
        self.mImagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        isFrontCamera = YES;
    }
    else
    {
        self.mImagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        isFrontCamera = NO;
    }

您可以在方向变化时按照自己的意愿设置控件。AppDelegate.m

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

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

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

现在进入cameraviewcontroller.m文件

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

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

我尝试覆盖所有要点。如果有任何疑问,请告诉我。希望能帮到你 :)
1)您需要在翻转摄像头按钮的点击事件中检查条件。
2)AppDelegate.h:声明一个对象,记录您的视频并创建一个属性。在这里,我以CameraViewController为例。
CameraViewController *objController;

现在在AppDelegate.m文件中:

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

现在使用这个实例来调用我上面展示的shouldAutorotate方法,并返回横向方向:
-(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中设置。

3) 我只是以cameraviewcontroller.m作为示例,它反映了您的录制类。在视频录制类的shouldAutorotate方法中,如果检测到的方向为纵向,则返回NO。这样UI界面就不会改变,而您可以保持横屏模式。


感谢您的评论。在我尝试实现之前有几个问题:1)“为了跟踪相机之间的切换,您可以使用标志” - 我无法确定这段代码应该放在哪里。2)“这是在方向更改时调用的委托。您可以使用特定类的对象来调用其shouldAutorotate方法,并根据方向设置相机控制位置。” - “self.objController” - 这是什么?我只有一个屏幕用于录制视频。它应该是横向的。其他屏幕是纵向的。3)cameraviewcontroller.m - 这是什么? - Dmitry Khryukin
我看到你的解决方案可以支持两个方向,只需要自定义控件。但是我需要它仅在横向模式下工作,以锁定它。 - Dmitry Khryukin
我已更新答案以满足您的查询。看看是否有帮助。 - Maverick
谢谢你的帮助。我将授予你赏金,因为你的答案是最好的。但是我现在无法检查它,所以我不能接受它。我稍后会检查它,并可能会问一些附加问题。 - Dmitry Khryukin
@Maverick 嘿,我在想设置 picker.showsCameraControls = NO; 是否会移除自动对焦功能? - iqueqiorio

0

尝试一下这个,也许它会帮助你

- (BOOL)shouldAutorotate
{
return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
 return UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
  return (interfaceOrientation == UIInterfaceOrientationLandscapeRight ||       interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

它会工作的...:)


感谢您的评论。我已经尝试了一下,但是出现了异常:*** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation',原因是:'preferredInterfaceOrientationForPresentation必须返回支持的界面方向!' - Dmitry Khryukin
如果我们将-(NSUInteger)supportedInterfaceOrientations替换为-(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; },它就能正常工作了。 - Naveen Shan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接