【问题标题】:iOS - Picker controller does not open Photo LibraryiOS - 选择器控制器不打开照片库
【发布时间】:2017-02-17 06:38:01
【问题描述】:

在我的应用程序中,我使用 UIActionSheet 和 UIImagePickerController。操作表打开选项(选择照片,选择视频)和图像选择器控制器打开库。该系统适用于 iPhone 设备测试,但对于 iPad,Action Sheet 运行良好,而 Picker Controller 则不行。

我已经为 iOS10、相机和照片库设置了所需的权限。这不可能是问题。

我选择照片的代码:

- (void)selectPhoto {

    self.imagePickerController.delegate = self;
    self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.imagePickerController.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
    [self presentViewController:self.imagePickerController animated:YES completion:nil];
}

也编写了必需的委托方法。正如我所提到的,一切都在 iPhone 中正常工作。

当我首先尝试打开照片库时,我收到以下警告消息:

[警告] <_uipopoverbackgroundvisualeffectview> 正在 要求对其不透明度进行动画处理。这将导致效果出现 直到不透明度恢复到 1 为止。

没有任何问题,应用仍在运行(未冻结)。但是,如果我尝试打开照片库,这次我会得到:

警告:尝试在 已经呈现(null)

然后,问题可能是弹出问题,但我找不到答案。 谢谢!

编辑:我的操作表委托方法:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
    switch (buttonIndex) {
        case 0:
            [self selectPhoto];
            break;
        case 1:
            [self selectVideo];
            break;
        default:
            break;
    }
}

编辑2:我找到that

在 iPad 上,您必须使用弹出框将浏览器界面呈现为 在 initWithContentViewController: 和 Presenting and 关闭 UIPopoverController 类参考中的弹出框。如果,在 iPad,您尝试以模态方式呈现浏览器界面 (全屏),系统抛出异常。

UIPopoverController 现已弃用。因此我应该使用另一种方式。如果有人可以帮助我解决这个问题,我会很高兴。

【问题讨论】:

  • 在提交UIImagePickerController之前,你有dismiss你的UIActionSheetController吗?
  • 我没有,但现在我做到了。还是一样。
  • 请显示关闭警报的附加代码。您需要确保在完成时显示图像选择器。否则将无法正常工作。

标签: ios objective-c uiimagepickercontroller uiactionsheet


【解决方案1】:

对于 iPad 使用代码打开 UIImagePicker

 [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                [self presentViewController:picker animated:NO completion:nil];
            }];

【讨论】:

    【解决方案2】:

    您可以在 Info.plist 文件中指定所有可可键的列表:

    https://developer.apple.com/library/mac/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html

    iOS 之前(iOS6、iOS7)已经需要访问麦克风、摄像头和媒体库的权限,但从 iOS10 开始,如果您不提供请求权限的说明,应用程序将会崩溃。

    【讨论】:

      【解决方案3】:

      在 iPad 上,您无法在屏幕上的 UIActionSheet 弹出框上显示 UIImagePickerController 弹出框。

      替换

      - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
      

      - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;
      

      它必须有效。

      并删除[actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];,只要你点击一个按钮,UIActionSheet 就会自动消失。

      【讨论】:

        【解决方案4】:

        要在 iPhone 和 iPad 上都显示图像选择器,您需要将其显示为弹出框。

        所以显示你的选择器的实现将更改为

        self.imagePickerController.delegate = self;
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        self.imagePickerController.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
        self.imagePickerController.modalPresentationStyle = UIModalPresentationPopover; //This is needed
        self.imagePickerController.sourceView = self.view; //This is needed
        [self presentViewController:self.imagePickerController animated:YES completion:nil];
        

        在展示选择器之前,请确保您的操作表已关闭。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-01
          • 2017-05-02
          • 2023-03-18
          • 2015-07-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多