Search code examples
iosswiftcalayeravcapturesessionios-camera

How to position an AVCaptureVideoPreviewLayer inside a view?


I would like to display the camera preview in a square UIView. This is what I am doing :

previewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.Portrait
mySquareView.layer.addSublayer(previewLayer)

The result is the camera preview automatically resizing to fit entirely in the view.

I want it to fit horizontally and to be cropped vertically.

How to do that ?


Solution

  • use previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill