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

问题描述 投票:0回答:1

我的应用程序使用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>

有任何想法吗?我很沮丧!

提前致谢。

ios objective-c uiimagepickercontroller
1个回答
0
投票

我没有看到cheking auth状态。如果需要,请查看这两种方法来检查状态,以便呈现选择器并请求身份验证。

首先(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.
}  
© www.soinside.com 2019 - 2024. All rights reserved.