【问题标题】:UIImagePickerController record video with landscape orientationUIImagePickerController 横向录制视频
【发布时间】:2013-03-13 04:31:02
【问题描述】:

我们如何强制 UIImagePickerController 控制器只在横向模式下录制视频?

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

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

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

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

#import "UIImagePickerController+NonRotating.h"

@implementation UIImagePickerController (NonRotating)

- (BOOL)shouldAutorotate
{

    return YES;
}

-(NSUInteger)supportedInterfaceOrientations

{
    return UIInterfaceOrientationMaskLandscape;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

@end

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

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

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

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

你有什么想法吗?

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

【问题讨论】:

  • 请不要关闭这个问题。您提供链接的问题没有正确答案,并且该 cmets 的所有解决方案都不起作用。
  • @iPatel 你读过我的问题吗?我已经告诉过这个链接。
  • 您是否尝试过设置视频方向? stackoverflow.com/questions/14811641/…
  • @Gotschi 没有,没试过。我不使用 AVCaptureConnection,我使用 UIImagePickerController
  • 您必须以某种方式旋转设备.... :(

标签: iphone ios objective-c video uiimagepickercontroller


【解决方案1】:

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

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

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

现在,您可以使用 start/stop 方法来录制视频:

点击开始录制时,

[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 是一个flag,它是在录制类的viewWillAppear 中设置的。在viewDidUnload 中设置false。或者可能是另一个类的viewWillAppear

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

【讨论】:

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

试试这个可能对你有帮助

- (BOOL)shouldAutorotate
{
return NO;
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}

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

它会起作用的......:)

【讨论】:

  • 感谢您的评论。已经尝试过,但出现异常:*** 由于未捕获的异常“UIApplicationInvalidInterfaceOrientation”而终止应用程序,原因:“preferredInterfaceOrientationForPresentation 必须返回受支持的界面方向!”
  • 如果我们添加 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; 它将起作用} 而不是 -(NSUInteger)supportedInterfaceOrientations { 返回 UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
  • 2011-12-14
  • 2011-01-06
  • 2015-10-05
  • 2013-01-15
相关资源
最近更新 更多