0

I'm trying to put an AVCaptureVideoPreviewLayer as a sublayer in a UIView element. I tried to set its frame to the UIView bounds by using:

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

When I tried to print the values of the frame and the bounds it gives me this:

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

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

In the actual application, it results in the sublayer not filling up the whole UIView element:

Error

Does anyone know how to resolve this?

Siemen Gijbels
  • 35
  • 1
  • 1
  • 17

1 Answers1

0

You need to

override func viewDidLayoutSubviews() {
  super.viewDidLayoutSubviews()
  cameraPreviewLayer?.frame = PreviewController.bounds
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87