【问题标题】:UIImagePickerController incorrectly states "No photos or libraries" in photo libraryUIImagePickerController 在照片库中错误地声明“没有照片或库”
【发布时间】:2019-08-31 01:01:45
【问题描述】:

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

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

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

    【讨论】:

    • 这似乎是我所缺少的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多