The*_*eak 0 iphone objective-c avfoundation ios
我正在尝试从ios设备的摄像头获取提要以在屏幕上显示,并且我不想使用ImagePickerController,因为我想对屏幕上的视图外观进行更多控制,并且希望对屏幕上的视图进行更多控制相机的光圈和采样率。
这是我在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];
Run Code Online (Sandbox Code Playgroud)
加载视图时,屏幕上什么也没有出现,并且我没有收到错误消息。知道我缺少什么吗?
您从未真正在显示的代码中启动捕获会话:
[captureSession startRunning];
Run Code Online (Sandbox Code Playgroud)
另外,我建议您查看Apple提供的AVCam示例代码,以了解他们如何设置捕获会话和预览层。它包含一些有关如何管理捕获会话的好技巧(它们将所有内容分发到自定义串行队列中)。例如,它在代码中包含以下注释:
// 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).
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1458 次 |
| 最近记录: |