0

我正在尝试将 AVCaptureVideoPreviewLayer 作为子层放在 UIView 元素中。我尝试使用以下方法将其框架设置为 UIView 边界:

cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
cameraPreviewLayer?.frame = PreviewController.bounds
PreviewController.layer.addSublayer(cameraPreviewLayer!)

当我尝试打印框架的值和边界时,它给了我这个:

print(PreviewController.bounds)
print(cameraPreviewLayer!.frame)

(0.0, 0.0, 375.0, 500.0)
(0.0, 0.0, 375.0, 500.0)

在实际应用中,会导致子层没有填满整个 UIView 元素:

错误

有谁知道如何解决这个问题?

4

1 回答 1

0

你需要

override func viewDidLayoutSubviews() {
  super.viewDidLayoutSubviews()
  cameraPreviewLayer?.frame = PreviewController.bounds
}
于 2019-03-08T19:39:44.053 回答