【问题标题】:iOS 11 - always opens photo library even the source type change it from .photoLibrary to .cameraiOS 11 - 始终打开照片库,即使源类型将其从 .photoLibrary 更改为 .camera
【发布时间】:2017-09-12 14:00:40
【问题描述】:

代码在 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)
}

【问题讨论】:

  • 它仍然发生在我身上。你能解决这个问题吗?
  • 你解决了这个问题吗?

标签: uiimagepickercontroller ios11 xcode9-beta


【解决方案1】:

我发现了问题所在。

MAIN:您必须在呈现 UIImagePickerController 之前设置 sourceType。 关于这一点,您可以在文档 UIImagePickerController 中阅读。

是的,您可以查看 sourceType 的文档,但文档页面上有关 sourceType 的信息对于 iOS 11 是错误的或不实际的。

结果:

  1. 首先你必须配置 UIImagePickerController
  2. 第二次展示。

在您的情况下,您只需要删除一行:

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

附:检查并在 Xcode 9 GM 上工作

【讨论】:

  • 您和#gile87 的答案都是正确的。我将#gile87 答案视为已接受,因为他首先得到了回答。他的名声也处于起步阶段。
【解决方案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 上也有同样的问题,但可以解决这个问题。也许您需要在buttonProfilePicPressed 方法中删除self.present(imgPicker, animated: true, completion: nil)

【讨论】:

  • 您和#Vladimir 的答案都是正确的。我认为您的回答已被接受,因为您首先得到了回答。您的声誉也处于初始阶段。
猜你喜欢
  • 2017-03-09
  • 1970-01-01
  • 1970-01-01
  • 2015-03-13
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多