IOS 11 - the photo library always opens, even the type of the source changes its .photoLibrary to .camera

The code works fine in iOS 10 and below. But in iOS 11, after canceling the photo library and opening the camera, it always opens the photo library. This only happens on iOS 11.

Code compiled in Xcode 9 Beta 4 .

Code below:

@IBAction func buttonProfilePicPressed(_ sender: UIButton) { let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet) alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in self.openCamera() })) alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in self.openGallary() })) alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil)) self.present(alert, animated: true, completion: nil) imgPicker.delegate = self self.present(imgPicker, animated: true, completion: nil) } func openCamera() { if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) { imgPicker.sourceType = UIImagePickerControllerSourceType.camera imgPicker.allowsEditing = true self.present(imgPicker, animated: true, completion: nil) } else { let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) self.present(alert, animated: true, completion: nil) } } func openGallary() { imgPicker.sourceType = UIImagePickerControllerSourceType.photoLibrary imgPicker.allowsEditing = true self.present(imgPicker, animated: true, completion: nil) } 
+5
source share
2 answers
 func startCameraFromViewController(_ viewController: UIViewController, withDelegate delegate: UIImagePickerControllerDelegate & UINavigationControllerDelegate) -> Void { if (UIImagePickerController.isSourceTypeAvailable(.camera) == false) { print("fail") } let cameraController = UIImagePickerController() cameraController.sourceType = .camera cameraController.allowsEditing = true cameraController.delegate = delegate present(cameraController, animated: true, completion: nil) } 

This is the code that works for me. Same issue on iOS 11, but working with that. Perhaps you need to remove the self.present(imgPicker, animated: true, completion: nil) buttonProfilePicPressed in buttonProfilePicPressed .

+3
source

I found that wrong.

MAIN: you must set sourceType before the UIImagePickerController view. You can read about this in the UIImagePickerController documentation.

Yes, you can see the documentation for sourceType , but the source information on the documentation page is incorrect or not relevant for iOS 11.

As a result:

  • First, you must configure the UIImagePickerController
  • Secondly, imagine it.

in your case you only need to delete one line:

 @IBAction func buttonProfilePicPressed(_ sender: UIButton) { ... self.present(imgPicker, animated: true, completion: nil) //REMOVE IT!!!!!111 } 

PS Verified and runs on Xcode 9 GM

+5
source

Source: https://habr.com/ru/post/1271731/


All Articles