【问题标题】:AVCaptureVideoPreviewLayer, video orientation stuckAVCaptureVideoPreviewLayer,视频方向卡住
【发布时间】:2014-01-23 16:55:02
【问题描述】:

我有一个使用 AVCaptureVideoPreviewLayer 的 iPhone 相机应用程序。当有人拍摄照片/静止图像时,它会显示在新的 ViewController 中。一旦这个视图控制器被解除,魔法就会发生。

整个应用旋转正常,无论是视图控制器;但有一个例外。如果我以一个方向拍摄照片,在新的 ViewController 中旋转设备,然后返回到 AVCaptureVideoPreviewLayer,整个层旋转得很好,除了图像,所以突然输入通过 previewLayer 横向呈现。

我已经检查过,并尝试为 PreviewLayer 设置框架,但这一切似乎都很好,我在调试时看到的值都是正确的。只是显示的图像偏斜。在使用过程中来回旋转设备可解决此问题。

以前有人见过这个吗,有没有人知道如何解决这个问题?

【问题讨论】:

  • 这是 iphone 还是 ipad 的?对于具有纵向模式的 iphone 棒。如果允许 iphone viewcontroller 旋转,我已经看到 AVCapture 发生了一些愚蠢的事情。
  • @SamBudda 它适用于 iPhone,正如我所说。除了那种情况,它工作得很好。

标签: ios objective-c rotation avcapture


【解决方案1】:

您可以在界面旋转时更改 previewLayer 连接的 videoOrientation

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    self.previewLayer.connection.videoOrientation = self.interfaceOrientation;
}

【讨论】:

  • 不幸的是,这不起作用,因为此时 viewController 不是活动的,因此不会调用此函数。然而,这确实为我指明了正确的方向。解决方案是编写自己的函数,将活动视图的方向传递给它,并在其顶部的活动 ViewController 改变方向时通过通知调用它。
【解决方案2】:

由于 didRotateFromInterfaceOrientation: 在 iOS 8 中已弃用,我在 viewDidLayoutSubviews 中更改了连接的视频方向

switch ([UIApplication sharedApplication].statusBarOrientation) {
    case UIInterfaceOrientationPortrait:
        self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
        break;
    case UIInterfaceOrientationPortraitUpsideDown:
        self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
        break;
    case UIInterfaceOrientationLandscapeLeft:
        self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
        break;
    case UIInterfaceOrientationLandscapeRight:
        self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
        break;
    default:
        break;
}

【讨论】:

    【解决方案3】:

    我过去也遇到过类似的问题。 关闭相机控制器后,您可以使用以下方法刷新视图旋转:

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *topView = [window.subviews objectAtIndex:0];
    [topView removeFromSuperview];
    [window addSubview:topView];        
    

    因此,删除显示窗口上最顶部的视图并在窗口上重置它,它应该正确刷新视图旋转。

    希望对你有帮助

    【讨论】:

    • 不幸的是,这似乎根本无法解决我的问题。不过感谢您的努力。
    【解决方案4】:

    也许,您可以尝试将其放入容器UIView,而不是更改AVCaptureVideoPreviewLayer 框架,并设置此容器的框架。 (我修复了 ÀVCaptureVideoPreviewLayerthis way : sizing directly PreviewLayer had no effect, but sizing its containingUIView 上的“调整大小”错误)。

    或者, 也许您可以在 viewController viewWillAppear (或 viewDidAppear ?)中强制定向

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        // try and force current orientation
        UIApplication *application = [UIApplication sharedApplication];
        UIInterfaceOrientation currentOrientation = application.statusBarOrientation;
        [application setStatusBarOrientation:currentOrientation animated:animated];
    }
    

    祝你好运!

    【讨论】:

      【解决方案5】:

      由于 interfaceOrientation 已被弃用,最新的 swift 2.0 解决方案应为:

      let previewLayerConnection = self.previewLayer.connection
      let orientation = AVCaptureVideoOrientation(rawValue: UIApplication.sharedApplication().statusBarOrientation.rawValue)
      previewLayerConnection.videoOrientation = orientation!
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-31
        • 1970-01-01
        • 2016-03-08
        • 1970-01-01
        • 1970-01-01
        • 2018-10-12
        • 2020-09-30
        相关资源
        最近更新 更多