【问题标题】:UIImagePickerController - turn video preview to landscapeUIImagePickerController - 将视频预览转为横向
【发布时间】:2014-07-25 08:21:50
【问题描述】:

我目前正在开发一个用于录制和编辑视频的应用程序。孔应用程序仅支持横向模式 - 所以我想在横向中使用相机控件。但预览屏幕总是切换回纵向。之前有人问过类似的问题 (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;
}

结果:

然后我用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;
}

结果:

我什至听了_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);
    }
}

结果:

如果有任何提示或建议,我将非常感谢我如何在横向模式下使用相机和预览,这样我就可以在不转动设备的情况下使用我的洞应用程序(设备将在三脚架中,而应用程序用过,所以不能转。)

谢谢你和最美好的祝愿!

【问题讨论】:

    标签: ios iphone camera orientation uiimagepickercontroller


    【解决方案1】:

    UIImage Picker View 不能仅在景观模式下使用,这是苹果所说的

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

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

    【讨论】:

      【解决方案2】:

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

      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];
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多