Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c AVCaptureSession预设照片和AVCaptureVideoPreviewLayer大小_Objective C_Ios_Camera - Fatal编程技术网

Objective c AVCaptureSession预设照片和AVCaptureVideoPreviewLayer大小

Objective c AVCaptureSession预设照片和AVCaptureVideoPreviewLayer大小,objective-c,ios,camera,Objective C,Ios,Camera,我初始化了一个AVCaptureSession,并将其预设为: AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init]; if (YES==[newCaptureSession canSetSessionPreset:AVCaptureSessionPresetPhoto]) { newCaptureSession.sessionPreset = AVCaptureSessionPresetPhoto;

我初始化了一个
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;
我的问题是:
如何获得在屏幕上显示所有
captureVideoPreviewLayer
层所需的确切
CGSize
?更准确地说,我需要的高度是
AVLayerVideoGravityResizeAspect
使
AVCaptureVideoPreviewLayer
适合
预览。大小


我尝试获得合适的AVCaptureVideoPreviewLayer大小。


非常感谢您的帮助

如果我理解正确,请尝试获取当前视频会话的宽度和高度。
您可以从
AVCaptureOutput
的outputSettings字典中获取它们。(使用
AVVideoWidthKey
AVVideoHeightKey

e、 g

更新:
另一个想法是从预览会话的图像缓冲区获取帧大小。
实现AVCaptureVideoDataOutputSampleBufferDelegate方法
captureOutput:didOutputSampleBuffer:fromConnection:
(不要忘记设置AVCaptureOutput的委托)


在使用AVCaptureSessionPresetPhoto进行一些研究后,AVCaptureVideoPreviewLayer尊重iPhone摄像头的3/4比例。因此,用简单的微积分很容易得到正确的高度。
例如,如果宽度为320,则适当的高度为:

320*4/3=426.6Weischel的代码不适合我。这个想法行得通,但代码行不通。以下是确实有效的代码:

    // Get your AVCaptureSession somehow. I'm getting mine out of self.videoCamera, which is a GPUImageVideoCamera
    // Get the appropriate AVCaptureVideoDataOutput out of the capture session. I only have one session, so it's easy.

    AVCaptureVideoDataOutput *output = [[[self.videoCamera captureSession] outputs] lastObject];
    NSDictionary* outputSettings = [output videoSettings];

    // AVVideoWidthKey and AVVideoHeightKey did not work. I had to use these literal keys.
    long width  = [[outputSettings objectForKey:@"Width"]  longValue]; 
    long height = [[outputSettings objectForKey:@"Height"] longValue];

    // video camera output dimensions are always for landscape mode. Transpose if your camera is in portrait mode.
    if (UIInterfaceOrientationIsPortrait([self.videoCamera outputImageOrientation])) {
        long buf = width;
        width = height;
        height = buf;
    }

    CGSize outputSize = CGSizeMake(width, height);

感谢gsempe为您提供的答案。我从几小时前开始就遇到同样的问题:) 我用这段代码解决了这个问题,以横向模式将它放在屏幕中央:

CGRect layerRect = [[[self view] layer] bounds];
[PreviewLayer setBounds:CGRectMake(0, 0, 426.6, 320)];
[PreviewLayer setPosition:CGPointMake(CGRectGetMidY(layerRect), CGRectGetMidX(layerRect))];
请注意,我必须反转CGRectGetMidY()和CGRectGetMidX()函数,以便在屏幕中有一个居中层

谢谢


朱利安

我尝试获取AVCaptureVideoPreviewLayer的大小。因为我只设置了一个stillImageoutput outputSettings,所以只提供了:avVideoCodeKey=jpeg;我更新了我的答案。也许你可以从预览图像中获得尺寸。使用AVCaptureSessionPresetPhoto进行一些研究后,AVCaptureVideoPreviewLayer尊重iPhone相机的3/4比例。所以用简单的微积分很容易得到正确的高度。不过,谢谢你的帮助
    // Get your AVCaptureSession somehow. I'm getting mine out of self.videoCamera, which is a GPUImageVideoCamera
    // Get the appropriate AVCaptureVideoDataOutput out of the capture session. I only have one session, so it's easy.

    AVCaptureVideoDataOutput *output = [[[self.videoCamera captureSession] outputs] lastObject];
    NSDictionary* outputSettings = [output videoSettings];

    // AVVideoWidthKey and AVVideoHeightKey did not work. I had to use these literal keys.
    long width  = [[outputSettings objectForKey:@"Width"]  longValue]; 
    long height = [[outputSettings objectForKey:@"Height"] longValue];

    // video camera output dimensions are always for landscape mode. Transpose if your camera is in portrait mode.
    if (UIInterfaceOrientationIsPortrait([self.videoCamera outputImageOrientation])) {
        long buf = width;
        width = height;
        height = buf;
    }

    CGSize outputSize = CGSizeMake(width, height);
CGRect layerRect = [[[self view] layer] bounds];
[PreviewLayer setBounds:CGRectMake(0, 0, 426.6, 320)];
[PreviewLayer setPosition:CGPointMake(CGRectGetMidY(layerRect), CGRectGetMidX(layerRect))];