Artem Krachulov
Artem Krachulov

Reputation: 597

AVCaptureVideoPreviewLayer video frame size

How can I get image size inside AVCaptureVideoPreviewLayer:

self.cameraPreviewLayer.frame = self.cameraView.frame; // (0.0, 0.0, 320.0, 568.0)

Image inside AVCaptureVideoPreviewLayer smaller than frame.

Upvotes: 10

Views: 4607

Answers (2)

Benny Davidovitz
Benny Davidovitz

Reputation: 1202

Swift Code

var captureSession : AVCaptureSession?

var captureInput : AVCaptureDeviceInput?{
    get{
        return self.captureSession?.inputs.first as? AVCaptureDeviceInput
    }
}

func doSomething(){
    guard let captureInput = captureInput else{ return }

    let dims : CMVideoDimensions = CMVideoFormatDescriptionGetDimensions(captureInput.device.activeFormat.formatDescription)
        //do whatever
}

Upvotes: 2

Leslie Godwin
Leslie Godwin

Reputation: 2658

You can't get the actual rendered size inside the AVCaptureVideoPreviewLayer frame. You'll have to calculate it.

Here's how you get the actual video dimensions:

AVCaptureDeviceInput *videoDeviceInput = // initialised already in your app

// Here you can get the video dimensions:
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(videoDeviceInput.device.activeFormat.formatDescription);

From here you can calc the aspect fitting rectangle inside the AVCaptureVideoPreviewLayer frame.

Upvotes: 10

Related Questions