iOS相机权限:iphone和ipad上的相机显示为黑色(UIImagePickerController发布iOS 9)



在我的应用程序中,我使用以下代码访问相机以更改用户的个人资料图片。但我无法使用相机。

 if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
 if(isIOS8SystemVersion)
        {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [self presentViewController:picker animated:YES completion:NULL];
            }];
        }
        else
        {
            [self presentViewController:picker animated:YES completion:NULL];
        }

此代码在iOS 7.x的iPhone 4(16gb)上运行良好。但在iOS 9中,显示的viewController显示为黑屏,我无法拍照。photoLibrary也是如此。相同的代码适用于同一设备中的其他应用程序。

我在Stack Overflow中讨论了许多问题,并尝试了许多解决方案。但它们对我不起作用。比如为UIImagePIcker控制器等做一个单例。

设置->隐私->摄像头->中,我看不到此部分列出的应用程序。

为了让iOS应用程序能够访问相机和照片,需要做什么?我遗漏了什么吗?

只需延迟打开相机的功能,它就会正常工作:D使用以下代码:

[self performSelector:@selector(showCamera) withObject:nil afterDelay:1.0];

-(void)showCamera
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    if(isIOS8SystemVersion)
    {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self presentViewController:picker animated:YES completion:NULL];
        }];
    }
    else
    {
        [self presentViewController:picker animated:YES completion:NULL];
    }
}

首先调用此方法并确定相机的权限

-(void)PermissionForCamera
{
    NSString *mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if(authStatus == AVAuthorizationStatusAuthorized)
    {
        NSLog(@"AVAuthorizationStatusAuthorized");
        // run your code for camera
        ImagePicker = [[UIImagePickerController alloc] init];
        [ImagePicker setDelegate:self];
        ImagePicker = UIImagePickerControllerSourceTypeCamera;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self ImagePicker animated:YES completion:nil];
        }];
    }
    else if(authStatus == AVAuthorizationStatusNotDetermined)
    {
        NSLog(@"AVAuthorizationStatusNotDetermined");
        // run your code for camera
        ImagePicker = [[UIImagePickerController alloc] init];
        [ImagePicker setDelegate:self];
        ImagePicker = UIImagePickerControllerSourceTypeCamera;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self ImagePicker animated:YES completion:nil];
        }];
    }
    else
    {
        // turn on the camera permission from settings
    }

}

最新更新