我在尝试使用相机时遇到了一些问题。问题是有些设备会在设置下向我显示相机条目,而有些则不会。在那些没有显示相机开关的设备上,我无法使用相机,因为它没有权限,而且它也没有出现在启用它们的设置下。
这是它在可用设备上的样子:
这就是它在不工作的设备中的样子。
当我截取这些屏幕截图时,应用程序应该已经请求权限,但它没有。
我还验证了这些设备没有启用限制。
有什么想法吗?
更新 1:添加代码
这是我用来显示相机的代码(它在自定义 View 下,而不是 native 相机 View Controller 下)
self.captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
if(videoInput)
{
[self.captureSession addInput:videoInput];
}
else
{
NSLog(@"Error: %@", error);
}
AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
[self.captureSession addOutput:metadataOutput];
[metadataOutput setMetadataObjectsDelegate:self
queue:dispatch_get_main_queue()];
[metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeQRCode]];
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
previewLayer.frame = self.view.layer.bounds;
UIView * previewView = [[UIView alloc] initWithFrame:self.view.frame];
[previewView.layer addSublayer:previewLayer];
[self.view addSubview:previewView];
[self.view sendSubviewToBack:previewView];
[self.captureSession startRunning];
最佳答案
您需要在打开 session 之前请求许可。使用
[AVCaptureDevice requestAccessForMediaType:completionHandler:]
关于iOS 相机权限不会出现在某些设备的设置下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32332313/