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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With