AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo在iPad上返回nil

时间:2019-01-22 12:42:11

标签: ios objective-c avfoundation

- (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总是 nil 。 iPad是装有iOS 12的A1474。

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

1 个答案:

答案 0 :(得分:0)

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

defaultDeviceWithMediaType的调用不再是nil。

也许这对发现它的其他人很有帮助。