在 Xcode 中打开 Popover 选项以在 iPad 上选择相机或照片库时出错

Posted

技术标签:

【中文标题】在 Xcode 中打开 Popover 选项以在 iPad 上选择相机或照片库时出错【英文标题】:Error while opening Popover Option to choose either Camera or photo Library on iPad in Xcode 【发布时间】:2020-08-24 15:21:48 【问题描述】:

我可以使用以下代码在 iPhone 上打开照片库:-

extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate 

@IBAction func TapOnImageView(sender: UITapGestureRecognizer) 
    
    //call Alert function
    self.showAlert()
    


//Show alert to selected the media source type.
private func showAlert() 

    let alert = UIAlertController(title: "Image Selection", message: "From where you want to pick this image?", preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: (action: UIAlertAction) in
        self.getImage(fromSourceType: .camera)
    ))
    alert.addAction(UIAlertAction(title: "Photo Album", style: .default, handler: (action: UIAlertAction) in
        self.getImage(fromSourceType: .photoLibrary)
    ))
    alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
    self.present(alert, animated: true, completion: nil)


//get image from source type
private func getImage(fromSourceType sourceType: UIImagePickerController.SourceType) 

    //Check is source type available
    if UIImagePickerController.isSourceTypeAvailable(sourceType) 

        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        imagePickerController.sourceType = sourceType
        self.present(imagePickerController, animated: true, completion: nil)
    


//MARK:- UIImagePickerViewDelegate.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) 

    self.dismiss(animated: true)  [weak self] in

        guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else  return 
        //Setting image to your image view
        
        self?.imageView.image = image
        self?.imageView.contentMode = .scaleToFill
        self?.image = self?.imageView.image
            
    


func imagePickerControllerDidCancel(_ picker: UIImagePickerController) 
    picker.dismiss(animated: true, completion: nil)



但是,当我在 iPad 上运行我的应用程序时,我收到以下错误:-

Thread 1: Exception: "Your application has presented a UIAlertController (<UIAlertController: 0x7ffd07875e00>) of style UIAlertControllerStyleActionSheet from App Name.ViewController (<App_Name.ViewController: 0x7ffd07635620>). The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem.  If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation."

谁能告诉我,我该怎么做才能在 Xcode 中打开 iPad 上的相机和照片库?感谢您的帮助!

更新:

解决方案:

var alertStyle = UIAlertController.Style.actionSheet
    
    if (UIDevice.current.userInterfaceIdiom == .pad) 
      alertStyle = UIAlertController.Style.alert
    
    
    let alert = UIAlertController(title: "Image Selection", message: "From where you want to pick this image?", preferredStyle: alertStyle)

【问题讨论】:

【参考方案1】:

您正在尝试在 iPad 上显示 UIAlertController,它需要将 modalStyle 设置为 UIModalPresentationPopover 及其 sourceViewsourceRect。详情请见this answer。

【讨论】:

谢谢你!我已经用我的答案更新了这个问题:)

以上是关于在 Xcode 中打开 Popover 选项以在 iPad 上选择相机或照片库时出错的主要内容,如果未能解决你的问题,请参考以下文章

iOS 7 Popover 控制器在 xcode 6 下崩溃

在html中如何链接表单提交以在新选项卡中打开并链接相对URL?

iOS5,popover 失去高度

Bootstrap Popover 忽略放置选项

尝试本地化 LaunchScreen.storyboard 以在 iOS 10 中使用 xcode 8.3.3 支持语言时出现问题

在 Xcode 11 中,如何在新选项卡或新窗口中快速打开文件?