使用警报在UIImagePickerController中选择图库或相机

时间:2017-01-18 15:20:36

标签: swift3 uialertview

我有一个可以工作的UIImagePickerController,但是我想让用户决定是拍摄图像还是从图书馆中挑选图像。所以这是我的代码尝试使用alert:

    @IBAction func cameraButton(_ sender: UIButton) {
    picker.allowsEditing = true
    let alert = UIAlertController(title: "Choose one of the following:", message: "", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { action in
            self.picker.sourceType = .photoLibrary
    }))
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { action in
        self.picker.sourceType = .camera
    }))
    self.present(alert, animated: true, completion: nil)
    present(picker, animated: true, completion: nil)
}

但是,一旦我点击警报中的某个操作,没有任何反应,它只是退出警报但没有显示我的选择器

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

我通过在这样的动作中展示来解决它:

    @IBAction func cameraButton(_ sender: UIButton) {
    picker.allowsEditing = true
    let alert = UIAlertController(title: "Choose one of the following:", message: "", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { action in
            self.picker.sourceType = .photoLibrary
            self.present(self.picker, animated: true, completion: nil)

    }))
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { action in
        self.picker.sourceType = .camera
        self.present(self.picker, animated: true, completion: nil)

    }))
    self.present(alert, animated: true, completion: nil)
}