如何在现有的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
本身的居中。