如何在自定义UIView中居中VideoPreviewLayer(Storyboard Swift)

问题描述 投票:0回答:1

如何在现有的UIView中将videoPreviewLayer居中?我已经尝试了以下方法,但是它比我想要的要小。



        var videoPreviewLayer : AVCaptureVideoPreviewLayer?

        let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
        do {
            let input = try AVCaptureDeviceInput(device: captureDevice!)
            captureSession = AVCaptureSession()
            captureSession?.addInput(input)
            videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
//            let frame : CGRect = self.scanView.layer.bounds
//            videoPreviewLayer?.frame = frame
            videoPreviewLayer?.frame = self.scanView.bounds
//            videoPreviewLayer?.bounds = self.view.bounds

            scanView.layer.addSublayer(videoPreviewLayer!)
            captureSession?.startRunning()
        } catch {
            print("error-jz")
        }

        capturePhotoOutput = AVCapturePhotoOutput()
        capturePhotoOutput?.isHighResolutionCaptureEnabled = true
        captureSession?.addOutput(capturePhotoOutput!)```
ios swift xcode uikit avfoundation
1个回答
0
投票

它应该在添加videoPreviewLayer的任何视图中居中。但是,当您说它小于所需的大小时,可能是因为您没有为播放器层设置视频重力。

videoPreviewLayer.videoGravity = .resizeAspectFill

这将使视频预览充满它所包含的整个视图。如果它仍然没有按您想要的方式居中,则可能必须修复UIView本身的居中。

© www.soinside.com 2019 - 2024. All rights reserved.