【发布时间】:2019-11-03 04:31:46
【问题描述】:
我正在使用UIImagePickerController 来选择拍照来源。此图片应设置为UIButton。
我知道您可以通过字符串将UIImage 添加到UIButton,例如
someButton.setImage(UIImage(named:...)),但我需要通过我在拍照时创建的变量来设置它。
@IBOutlet weak var addPictureOutlet: UIButton!
func chooseSource() {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let kameraAction = UIAlertAction(title: "Kamera",
style: .default) { (action) in
imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
}
let libraryAction = UIAlertAction(title: "Galerie",
style: .default) { (action) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel)
let alert = UIAlertController(title: "Quelle",
message: "Wählen sie eine Quelle für die Klausur.",
preferredStyle: .actionSheet)
alert.addAction(kameraAction)
alert.addAction(libraryAction)
alert.addAction(cancelAction)
self.present(alert, animated: true) {
}
}
@IBAction func addPicture(_ sender: UIButton) {
chooseSource()
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image1 = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
addPictureOutlet.setImage(image1, for: .normal)
}
}
我试过这个,但我真的不知道为什么它不起作用,编译或运行时没有错误,UIButton 的UIImage 没有改变。
【问题讨论】:
-
首先检查变量是否正在获取图像的输入或者它是空的
-
永远不会调用 imagePicker 函数。
-
我认为您缺少委托,请参阅我的以下答案
标签: swift uibutton uiimageview uiimagepickercontroller