代码之家  ›  专栏  ›  技术社区  ›  Divya Saraswati

如果已授予photolibrary权限,但未授予相机权限,则相机将打开并拍摄空白照片

  •  1
  • Divya Saraswati  · 技术社区  · 8 年前

    我的代码是这样的

    -(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 回复  |  直到 8 年前
        1
  •  0
  •   Himanth    8 年前

    试着这样做:

    对于 :

        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];
     }
    

    对于 照片库

    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];
    }
    

    -(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 方法