【问题标题】:Using an alert to choose between image gallery or camera in UIImagePickerController在 UIImagePickerController 中使用警报在图像库或相机之间进行选择
【发布时间】:2017-01-18 15:20:36
【问题描述】:

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

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

但是,一旦我单击警报中的某个操作,什么都不会发生,它只是退出警报但不显示我的选择器

感谢任何帮助

【问题讨论】:

    标签: swift3 uialertview


    【解决方案1】:

    我通过在这样的动作中呈现来解决它:

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

    【讨论】:

      猜你喜欢
      • 2011-09-07
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多