如果给予了照片库许可并且没有给出相机许可,则相机打开并拍摄空白照片

时间:2017-08-02 09:02:29

标签: ios objective-c uiimagepickercontroller phphotolibrary

我已经拒绝了相机的许可,但是之前获得过photolibrary的许可。但相机打开黑屏。我可以看到拍照的选项。有没有办法不打开相机。

我的代码就像这样

-(void)showImagePickerWithSoureType:(UIImagePickerControllerSourceType)type
{
    if([UIImagePickerController isSourceTypeAvailable:type])
    {
        pickerObj = [UIImagePickerController new];
        pickerObj.sourceType = type;
        UIViewController *topMostViewController = [CommonFunctions getTopMostViewControllerFromRootViewController:[CommonFunctions getAppDelegateObject].window.rootViewController];
        dispatch_async(dispatch_get_main_queue(), ^{
           [topMostViewController presentViewController:pickerObj animated:YES completion:NULL];
            pickerObj.delegate = self;
            pickerObj.editing = true;
            pickerObj.allowsEditing = true;
        });


        if([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusNotDetermined)
        {
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                switch (status) {
                    case PHAuthorizationStatusAuthorized:
                        break;
                    case PHAuthorizationStatusRestricted:
                        [self imagePickerControllerDidCancel:pickerObj];
                        break;
                    case PHAuthorizationStatusDenied:
                        [self imagePickerControllerDidCancel:pickerObj];
                        break;
                    default:
                        break;
                }
            }];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

试试这样:

对于相机

    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if(authStatus == AVAuthorizationStatusAuthorized)
    {
        [self accessCamera];
    }
    else if(authStatus == AVAuthorizationStatusNotDetermined)
    {

        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
         {
             if(granted)
             {
                 [self accessCamera];
             }
             else
             {
                 [self deniedCamera];
             }
         }];
    }
else
 {
            [self deniedCamera];
 }

PhotoLibrary

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

if (status == PHAuthorizationStatusAuthorized) {
    [self accessPhotoLibrary];
}
else if (status == PHAuthorizationStatusNotDetermined) {

    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

        if (status == PHAuthorizationStatusAuthorized) {
            [self accessPhotoLibrary];
        }else {
            [self deniedPhotoLibrbary];
        }
    }];
}
else  {
    [self deniedPhotoLibrbary];
}

调用pickerView委托的方法

-(void)accessCamera{        


    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self presentViewController:picker animated:YES completion:NULL];
        });
}


-(void)accessPhotoLibrary{
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.allowsEditing = YES;
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self presentViewController:picker animated:YES completion:NULL];
        });

}

然后你可以从didFinishPickingMediaWithInfo方法获得图像。