adri567
adri567

Reputation: 661

camera roll image is bigger than the camera preview

I am creating a camera app. I can take a image and the image is passing back to a view controller to show the image. It also saved to the camera roll. If I compare the image with the camera preview to the saved image, it seems like that the camera preview is a little bit zoomed in.

Image from camera preview: enter image description here

Image from Gallery: enter image description here

This is my code so far from the camera preview:

let cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    cameraPreviewLayer.videoGravity = .resizeAspectFill
    cameraPreviewLayer.connection?.videoOrientation = .portrait
    cameraPreviewLayer.frame = view.frame
    view.layer.insertSublayer(cameraPreviewLayer, at: 0)
    captureSession.startRunning()

How can I make the camera preview to the same size as the saved image?

Upvotes: 1

Views: 1169

Answers (2)

HUNG TRAN DUY
HUNG TRAN DUY

Reputation: 29

If you want the captured Image to have the same size as the camera preview. You should set videoGravity = .resize

Upvotes: 0

Frank Rupprecht
Frank Rupprecht

Reputation: 10398

The culprit is the videoGravity setting. .resizeAspectFill will resize the preview to fill the whole view, even when that means cropping content.

If you want to see the whole frame, set it to .resizeAspect. This will introduce black bars, though.

Upvotes: 0

Related Questions