UIImagePickerController错误地指出照片库中的“没有照片或库”

时间:2019-04-09 17:00:17

标签: ios objective-c uiimagepickercontroller

我的应用程序使用Apple的UIImagePickerController从照片库获取图像。在某些iOS 12手机上,将显示一个空的图像选择器,并显示消息“无照片或视频”。问题在于手机库中有照片。

拍摄新照片并将其保存在应用程序外部,可以解决问题;完成此操作后,该应用即可正常从照片库中进行选择。

以下是传递到UIAlertController(一个操作表,询问是从相机还是从库中选取)的块:

    void (^chooseExistingAction)(void) = ^() {
        [self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    };

以下是呈现图像选择器的方法:

- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType {
    if (![UIImagePickerController isSourceTypeAvailable:sourceType]) {
        [self showAlertWithMessage:ImageSourceNotAvailableAlert];
    } else {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = sourceType;
        imagePicker.delegate = self;
        imagePicker.navigationBar.tintColor = self.navigationController.navigationBar.tintColor;
        [self presentViewController:imagePicker animated:YES completion:NULL];
    }
}

最后,这是应用程序Info.plist中的相关键(这些值已略作编辑):

    <key>NSCameraUsageDescription</key>
    <string>This information will be used to provide visual details about [etc]</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>This information will be used to provide visual details about [etc]</string>

有什么想法吗?我被弄糊涂了!

谢谢。

1 个答案:

答案 0 :(得分:0)

我没有看到正在验证身份的状态。看看这两种检查状态的方法,即在需要选择器时显示状态并根据需要请求身份验证。

第一(stole from this answer):

NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus == AVAuthorizationStatusAuthorized) {
  // do your logic
} else if(authStatus == AVAuthorizationStatusDenied){
  // denied
} else if(authStatus == AVAuthorizationStatusRestricted){
  // restricted, normally won't happen
} else if(authStatus == AVAuthorizationStatusNotDetermined){
  // not determined?!
  [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
    if(granted){
      NSLog(@"Granted access to %@", mediaType);
    } else {
      NSLog(@"Not granted access to %@", mediaType);
    }
  }];
} else {
  // impossible, unknown authorization status
}

第二(stole from this answer):

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

if (status == PHAuthorizationStatusAuthorized) {
     // Access has been granted.
} else if (status == PHAuthorizationStatusDenied) {
     // Access has been denied.
} else if (status == PHAuthorizationStatusNotDetermined) {
     // Access has not been determined.
     [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
         if (status == PHAuthorizationStatusAuthorized) {
             // Access has been granted.         
         } else {
             // Access has been denied.
         }
     }];  
} else if (status == PHAuthorizationStatusRestricted) {
     // Restricted access - normally won't happen.
}