AVCaptureVideoPreviewLayer视频帧大小

时间:2015-04-30 07:44:31

标签: ios swift video avfoundation frame

如何在AVCaptureVideoPreviewLayer内获取图片尺寸:

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

小于帧的AVCaptureVideoPreviewLayer内的图像。

2 个答案:

答案 0 :(得分:4)

您无法在AVCaptureVideoPreviewLayer框架内获得实际渲染的尺寸。你必须计算它。

以下是获取实际视频尺寸的方法:

AVCaptureDeviceInput *videoDeviceInput = // initialised already in your app

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

从这里你可以在AVCaptureVideoPreviewLayer框架内计算方面拟合矩形。

答案 1 :(得分:0)

快捷代码

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
}