iPhone Xs Max:添加到View的子图层时,AVCaptureVideoPreviewLayer占用的空间少于iPhone X的安全区域-为什么? [英] iPhone Xs Max: AVCaptureVideoPreviewLayer when added to sublayer of View occupies less than the safe area of the iphone x - why?

查看:152
本文介绍了iPhone Xs Max:添加到View的子图层时,AVCaptureVideoPreviewLayer占用的空间少于iPhone X的安全区域-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有新iPhone Xs Max的单视图应用程序构建自定义相机。参考下面的屏幕截图,可以看出AVCaptureVideoPreviewLayer中显示的视频输出所占的空间小于iPhone x上屏幕的安全区域。如何计算尚未占用的空间?

I am trying to build a custom camera using a single view application with the new iPhone Xs Max. With reference to the screenshot below, it can be seen that the video output displayed in AVCaptureVideoPreviewLayer occupies less than the safe area of the screen on the iPhone x. How do I calculate the space that has not been occupied?

我如何知道AVCaptureVideoPreviewLayer占用的空间少于安全区域?好吧,顶部按钮的顶部边界固定在安全区域的顶部边界。底部按钮类似地锚定在底部安全区域的底部。

How do I know that the AVCaptureVideoPreviewLayer occupies less than the safe area? Well, the top border of the top 'Button' is anchored to the top border of the safe area. The bottom button is similarly anchored to the bottom of the bottom safe area. Yet there is huge overlap!

问题:

单视图应用程序的布局:按钮顶部约束

The layout of single view application: Button top constraint

底部白色按钮底部约束:

Bottom white button bottom constraint:

所有其他布局约束:

SWIFT:

class ViewController: UIViewController, UIImagePickerControllerDelegate, AVCaptureVideoDataOutputSampleBufferDelegate  {

var previewLayer = AVCaptureVideoPreviewLayer.init()
var captureSession: AVCaptureSession!
var previewFrame = CGRect.init()
var safeAreaFrame: CGRect = CGRect.init()



override func viewDidLoad() {
    super.viewDidLoad()

      let screenSize = UIScreen.main.bounds
      // This is (0.0, 0.0, 414.0, 896.0)

      startAVCaptureSession()

}



override func viewDidLayoutSubviews() {
    print("viewDidLayoutSubviews ...")

    let sAreaFrame = UIApplication.shared.windows[0].safeAreaLayoutGuide.layoutFrame
    self.safeAreaFrame = sAreaFrame

    // self.safeAreaFrame - This is (0.0, 44.0, 414.0, 818.0)


    self.previewLayer.frame = view.bounds // // I have tried  self.previewLayer.frame = self.safeAreaFrame 
    self.previewFrame = previewLayer.frame
}




func startAVCaptureSession() {
    print("START CAPTURE SESSION!!")

    // Setting Up a Capture Session
    self.captureSession = AVCaptureSession()
    captureSession.beginConfiguration()

    // Configure input
    let videoDevice = AVCaptureDevice.default(for: .video)

    guard
        let videoDeviceInput = try? AVCaptureDeviceInput.init(device: videoDevice!) as AVCaptureInput,
        self.captureSession.canAddInput(videoDeviceInput)else {return}

    self.captureSession.addInput(videoDeviceInput)

    // Capture video output
    let videoOutput = AVCaptureVideoDataOutput.init()
    guard self.captureSession.canAddOutput(videoOutput) else {return}
    videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue.init(label: "videoQueue"))
    self.captureSession.addOutput(videoOutput)


    // start
    self.captureSession.commitConfiguration()
    self.captureSession.startRunning()


    // Display camera preview
    self.previewLayer = AVCaptureVideoPreviewLayer.init(session: self.captureSession)

    // Use 'insertSublayer' to enable button to be viewable
    view.layer.insertSublayer(self.previewLayer, at: 0)



}





}

项目链接:
https:/ /github.com/babylon123/RectangleCapture

推荐答案

它与长宽比有关。如果添加此行,它应该可以正常工作:

It has to do with the aspect ratio. If you add this line it should work:

self.previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

self.previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

这篇关于iPhone Xs Max:添加到View的子图层时,AVCaptureVideoPreviewLayer占用的空间少于iPhone X的安全区域-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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