首页 > 解决方案 > AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo 在 ipad 上返回 nil

问题描述

- (void)setupScanningSession {
// Initalising hte Capture session before doing any video capture/scanning.

NSError *error;

self.captureSession = [[AVCaptureSession alloc] init];
self.captureSession.sessionPreset = AVCaptureSessionPresetMedium;

// Set camera capture device to default and the media type to video.
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Set video capture input: If there a problem initialising the camera, it will give am error.
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];

if (!input) {
    NSLog(@"Error connecting camera: %@", [error localizedDescription]);
    return;
}
// Adding input souce for capture session. i.e., Camera
[self.captureSession addInput:input];

当我尝试在真实设备上运行应用程序时,captureDevice 始终为零。iPad 是带有 iOS 12 的 A1474。

同一段代码在我的 iPhone 8 上完美运行。

标签: iosobjective-cavfoundation

解决方案


在把头发拉过这个之后,我终于找到了。我正在使用由我的公司管理的 iPad,并且相机被管理配置文件禁用。删除此配置文件后,iOS 内置相机应用程序也再次出现。

defaultDeviceWithMediaType 的调用不再为零。

也许这对发现此问题的其他人有所帮助。


推荐阅读