Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 11 - always opens photo library even the source type change it from .photoLibrary to .camera

The code is perfectly working in iOS 10 and below. But, in iOS 11 after cancel the photo library and open the camera its always opens the photo library. This is only happening in iOS 11.

Code is 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)
}
like image 622
Mohd Sadham Avatar asked Sep 12 '17 14:09

Mohd Sadham


People also ask

What are the changes to photo library permission in iOS 14?

Meanwhile, from the user’s perspective, it is a very welcome change. Users can now have more control over the privacy of their photos, limiting the number of photos an app can access. With this new permission status, the way to handle photo library permission in iOS 14 is very different from the previous iOS versions.

How to observe photo library change when selected photos change?

Head back to the viewDidLoad () method and add the following line of code: That’s it for observing photo library change. Our custom image picker should now be able to display the correct amount of selected photos every time selected photos changes.

What happens when you turn on iCloud Photos for multiple libraries?

If you designate a new library as the System Photo Library and then turn on iCloud Photos, the photos and videos in the new library will merge with those already in your iCloud Photos. If you want to keep the contents of your photo libraries separate, don’t turn on iCloud Photos for more than one library in Photos.

What is the system photo library on a Mac?

Designate a System Photo Library in Photos. If you have multiple photo libraries on your Mac, you can choose one to be the System Photo Library. The System Photo Library is the only library that can be used with iCloud Photos, Shared Albums, and My Photo Stream.


2 Answers

I found what's wrong.

MAIN: You must set sourceType before you present UIImagePickerController. About this you can read in documentation UIImagePickerController.

Yes, you can see documentation for sourceType, but information about sourceType on documentation page is wrong or not actual for iOS 11.

In result:

  1. Firstly you must configure UIImagePickerController
  2. Secondly present it.

in your case you need remove only one row:

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

P.S. Checked and work on Xcode 9 GM

like image 158
Vladimir Prigarin Avatar answered Jan 01 '23 02:01

Vladimir Prigarin


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 problem on iOS 11, but working with this. Maybe you need to remove self.present(imgPicker, animated: true, completion: nil) in buttonProfilePicPressed method.

like image 20
gile87 Avatar answered Jan 01 '23 01:01

gile87