gpt4 book ai didi

ios - 无法显示 AVCaptureVideoPreviewLayer

转载 作者:行者123 更新时间:2023-11-28 18:34:04 28 4
gpt4 key购买 nike

我正在尝试从 ios 设备的摄像头获取提要以显示在屏幕上,但我不想使用 ImagePickerController,因为我想更好地控制屏幕上 View 的外观,并且我想要更多地控制相机的光圈和采样率。

这是我在 UIViewController 的 viewDidLoad 方法中使用的代码:

//Setting up the camera
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
//This gets the back camera, which is the default video avcapturedevice
AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *cameraInput = [AVCaptureDeviceInput deviceInputWithDevice:camera error:&error];
if (cameraInput) {
[captureSession addInput:cameraInput];
}
else {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Camera not found" message:@"Your device must have a camera in order to use this feature" delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
UIView* cameraView = [[UIView alloc] initWithFrame:self.view.bounds];
previewLayer.frame = cameraView.bounds;
previewLayer.videoGravity = @"AVLayerVideoGravityResizeAspect";
[cameraView.layer addSublayer:previewLayer];
[self.view addSubview:cameraView];

然而当 View 加载时屏幕上没有任何显示,我也没有收到错误消息。知道我错过了什么吗?

最佳答案

您实际上从未在显示的代码中启动捕获 session :

[captureSession startRunning];

此外,我建议您查看 the AVCam sample code that Apple has provided查看他们如何设置捕获 session 和预览层。它包含一些关于如何管理捕获 session 的好技巧(它们将所有内容分派(dispatch)到自定义串行队列)。例如,它在代码中包含这些注释:

// In general it is not safe to mutate an AVCaptureSession or any of its inputs, outputs, or connections from multiple threads at the same time.
// Why not do all of this on the main queue?
// -[AVCaptureSession startRunning] is a blocking call which can take a long time. We dispatch session setup to the sessionQueue so that the main queue isn't blocked (which keeps the UI responsive).

关于ios - 无法显示 AVCaptureVideoPreviewLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22386835/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com