我初始化了AVCaptureSession
,并像这样预设了它:
AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init];
if (YES==[newCaptureSession canSetSessionPreset:AVCaptureSessionPresetPhoto]) {
newCaptureSession.sessionPreset = AVCaptureSessionPresetPhoto;
} else {
// Error management
}
然后我设置一个
AVCaptureVideoPreviewLayer
:self.preview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/*426*/)];
CALayer *previewLayer = preview.layer;
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
captureVideoPreviewLayer.frame = previewLayer.frame;
[previewLayer addSublayer:captureVideoPreviewLayer];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspect;
我的问题是:
如何获得在屏幕上显示所有
CGSize
层所需的确切captureVideoPreviewLayer
?更精确地说,我需要高度,因为AVLayerVideoGravityResizeAspect
使AVCaptureVideoPreviewLayer
适合preview.size
吗?我尝试获取合适的AVCaptureVideoPreviewLayer大小。
非常感谢您的帮助
最佳答案
经过对AVCaptureSessionPresetPhoto的研究后,AVCaptureVideoPreviewLayer尊重iPhone相机的3/4比例。因此,通过简单的演算就可以轻松确定合适的高度。
例如,如果宽度为 320 ,则足够的高度为:
320 * 4/3 = 426.6