Когда я нажимаю на кнопку с изображением, чтобы UIImagePicker galary выбрал изображение на iPad, приложение перестало работать. - PullRequest
1 голос
/ 31 марта 2020

Я использую 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)
 }

я проверил Cra sh указывает, давая точку останова, когда дело доходит до openPhotoGallery(), затем происходит сбой.

UIViewAlertForUnsatisfiableConstraints, чтобы перехватить это в отладчике.

Методы в категории UIConstraintBasedLayoutDebugging в UIView в списке также может быть полезным.

, пожалуйста, помогите мне с кодом. на пи c изображения из галереи iPad.

1 Ответ

1 голос
/ 31 марта 2020

Для iPad добавьте свой код:

(под строкой let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet))

actionSheetController.popoverPresentationController?.sourceView = self.yourView

Надеюсь, это поможет ...

...