簡體   English   中英

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

[英]How To Center VideoPreviewLayer in Custom UIView (Storyboard Swift)

如何在現有 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!)```

它應該以您添加 videoPreviewLayer 的任何視圖為中心。 但是,當您說它比您想要的要小時,那可能是因為您沒有為播放器層設置視頻重力。

videoPreviewLayer.videoGravity = .resizeAspectFill

這將使視頻預覽充滿它所包含的整個視圖。 如果它仍然沒有按照您想要的方式居中,您可能需要修復UIView本身的居中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM