AVCaptureVideoPreviewLayer横向定位 [英] AVCaptureVideoPreviewLayer landscape orientation

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

问题描述

如何让'AVCaptureVideoPreviewLayer'在横向方向正确显示?它在纵向工作正常,但不旋转,并在父视图控制器处于横向时显示旋转的摄像头捕获。

How can I get 'AVCaptureVideoPreviewLayer' to display properly in landscape orientation? It works fine in portrait but doesn't rotate, and shows a rotated camera capture when the parent view controller is in landscape orientation.

推荐答案

首先,答案

- (void)viewWillLayoutSubviews {
  _captureVideoPreviewLayer.frame = self.view.bounds;
  if (_captureVideoPreviewLayer.connection.supportsVideoOrientation) {
    _captureVideoPreviewLayer.connection.videoOrientation = [self interfaceOrientationToVideoOrientation:[UIApplication sharedApplication].statusBarOrientation];
  }
}

- (AVCaptureVideoOrientation)interfaceOrientationToVideoOrientation:(UIInterfaceOrientation)orientation {
  switch (orientation) {
    case UIInterfaceOrientationPortrait:
        return AVCaptureVideoOrientationPortrait;
    case UIInterfaceOrientationPortraitUpsideDown:
        return AVCaptureVideoOrientationPortraitUpsideDown;
    case UIInterfaceOrientationLandscapeLeft:
        return AVCaptureVideoOrientationLandscapeLeft;
    case UIInterfaceOrientationLandscapeRight:
        return AVCaptureVideoOrientationLandscapeRight;
    default:
        break;
  }
  NSLog(@"Warning - Didn't recognise interface orientation (%d)",orientation);
  return AVCaptureVideoOrientationPortrait;
}

我在这个问题上遇到过几个SO帖子,但是不能找到任何简单的解释,所以我认为我会分享我自己的。

I've come across several SO posts on this matter, and couldn't find any simple explanation so I thought I'd share my own.

如果您关注此问题的Apple示例,当您轮换您的问题时,您将遇到两个潜在的问题iOS设备到横向

If you follow Apple's sample on the matter, you'll run into two potential problems when you rotate your iOS device to landscape


  1. 捕获将显示为旋转(就像你将相机保持90度一样)

  2. 它不会完全跨越它的设定界限

这里的问题是'CALayer'不支持自动旋转因此,与你添加为子视图的'UIView'不同,当它的父'UIView'旋转时它不会旋转。因此,每当父视图的边界发生变化时,您必须手动更新其框架(不是父视图的框架,因为框架在旋转后保持不变)。这是通过覆盖容器视图控制器中的'viewWillLayoutSubviews'来实现的。

The problem here is that 'CALayer' doesn't support autorotation hence, unlike a 'UIView' you'd add as a subview, it won't rotate when its parent 'UIView' rotates. Therefore, you have to manually update its frame every time the parent view's bounds changes (not parent view's frame since frame stays the same after rotation). This is achieved by overriding 'viewWillLayoutSubviews' in the container view controller.

其次,你应该使用'videoOrientation'属性来告知AVFoundation方向,以便正确地进行预览。

Secondly, you should use 'videoOrientation' property to inform AVFoundation about the orientation so it does the preview properly.

希望这会有所帮助。

这篇关于AVCaptureVideoPreviewLayer横向定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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