【问题标题】:Error while opening Popover Option to choose either Camera or photo Library on iPad in Xcode在 Xcode 中打开 Popover 选项以在 iPad 上选择相机或照片库时出错
【发布时间】: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)

【问题讨论】:

    标签: ios swift xcode


    【解决方案1】:

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

    【讨论】:

    • 谢谢你!我已经用我的答案更新了这个问题:)
    猜你喜欢
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 2015-10-20
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多