iOS 11 — всегда открывает библиотеку фотографий, даже если тип источника изменился с .photoLibrary на .camera

Код отлично работает в iOS 10 и ниже. Но в iOS 11 после отмены библиотеки фотографий и открытия камеры всегда открывается библиотека фотографий. Это происходит только в iOS 11.

Код скомпилирован в Xcode 9 Beta 4.

Код ниже:

@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)
}

person Mohd Sadham    schedule 12.09.2017    source источник
comment
Это все еще происходит со мной. Вы смогли это решить?   -  person Alex Tau    schedule 20.09.2017
comment
Вы это исправили?   -  person Vladimir Prigarin    schedule 21.09.2017


Ответы (2)


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)

    }

Это код, который работает для меня. Та же проблема на iOS 11, но работает с этим. Возможно, вам нужно удалить self.present(imgPicker, animated: true, completion: nil) в методе buttonProfilePicPressed.

person gile87    schedule 21.09.2017
comment
И ваш ответ, и ответ #Владимир верны. Я принимаю ваш ответ как принятый, потому что вам ответили первым. Также ваша репутация находится в начальной стадии. - person Mohd Sadham; 25.09.2017

Я нашел, что не так.

ГЛАВНОЕ: Вы должны установить sourceType перед тем, как представить UIImagePickerController. Об этом вы можете прочитать в документации UIImagePickerController.

Да, вы можете увидеть документацию для sourceType, но информацию о sourceType в документации страница неверна или не актуальна для iOS 11.

Результат:

  1. Во-первых, вы должны настроить UIImagePickerController
  2. Во-вторых, представить его.

в вашем случае вам нужно удалить только одну строку:

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

P.S. Проверено и работает на Xcode 9 GM

person Vladimir Prigarin    schedule 22.09.2017
comment
И ваш ответ, и ответ #gile87 верны. Я принимаю ответ #gile87 как принятый, потому что ему ответили первым. Также его репутация находится в начальной стадии. - person Mohd Sadham; 25.09.2017