当我点击 ImagePicker 画廊的图像按钮以选择 iPad 中的图像时,应用程序崩溃了,为什么?

When i tap on image button for UIImagePicker galary to pick image in iPad then app is crashing why?

我正在使用 UIImagePicker 来选取图像,它可以很好地为 iPhone 画廊选取图像。但是当我在 iPad 中的 xcode 中测试应用程序时,当我点击图像按钮时,应用程序崩溃了,我不知道为什么。

代码如下:

 @IBAction func addProfileBtnAction(_ sender: Any) {

   let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

   //Create and add the Cancel action
   let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
       //Just dismiss the action sheet
   }
   actionSheetController.addAction(cancelAction)
   //Create and add first option action
   let takePictureAction: UIAlertAction = UIAlertAction(title: "TakePhoto", style: .default) { action -> Void in
       //Code for launching the camera goes here
       self.openCameraPicker()
   }
   actionSheetController.addAction(takePictureAction)
   //Create and add a second option action
   let choosePictureAction: UIAlertAction = UIAlertAction(title: "ChooseFromLibrary", style: .default) { action -> Void in
       //Code for picking from Gallery  goes herece
       self.openPhotoGallery()
   }
   actionSheetController.addAction(choosePictureAction)
   //Present the AlertController
   self.present(actionSheetController, animated: true, completion: nil)
   }
   func openCameraPicker() {
   picker.sourceType = UIImagePickerController.SourceType.camera
   picker.cameraCaptureMode = .photo
   picker.modalPresentationStyle = .fullScreen
   present(picker,animated: true,completion: nil)
   }
  func openPhotoGallery() {
   picker.sourceType = .photoLibrary
   picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
   present(picker, animated: true, completion: nil)
   }
  // MARK: - UIImagePickerControllerDelegate Methods
  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
   if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
       //pickedImage  = image
       imgPick.image = image
   }
   dismiss(animated: true, completion: nil)
  }
 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
   dismiss(animated: true, completion: nil)
 }

我已经通过给断点检查了崩溃点,当涉及到openPhotoGallery()然后它崩溃了。

UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.

The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

请帮我写代码。也可以从 iPad 图库中获取图片。

对于 iPad 添加您的代码:

(在let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)行下)

actionSheetController.popoverPresentationController?.sourceView = self.yourView

希望对您有所帮助...