UIImagePickerController 在照片库中错误地声明 "No photos or libraries"

UIImagePickerController incorrectly states "No photos or libraries" in photo library

我的应用程序使用 Apple 的 UIImagePickerController 从照片库中获取图像。在 iOS 12 phone 秒后,会显示一个空的图像选择器,并显示消息 "No photos or videos"。问题是 phone 的图库中有照片。

在应用程序外拍一张新照片并保存,问题就解决了;完成后,应用程序可以正常从照片库中选择。

这是传递给 UIAlertController 的块(一个动作 sheet 询问是否从相机或库中拾取):

    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>

有什么想法吗?我很困惑!

提前致谢。

我没有看到 cheking 身份验证状态。查看这两种方法,在呈现选择器之前检查状态,并在需要时请求授权。

第一个(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.
}