【问题标题】:How do I add an image taken by camera or galery to a button?如何将相机或图库拍摄的图像添加到按钮?
【发布时间】: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)
    }
}

我试过这个,但我真的不知道为什么它不起作用,编译或运行时没有错误,UIButtonUIImage 没有改变。

【问题讨论】:

  • 首先检查变量是否正在获取图像的输入或者它是空的
  • 永远不会调用 imagePicker 函数。
  • 我认为您缺少委托,请参阅我的以下答案

标签: swift uibutton uiimageview uiimagepickercontroller


【解决方案1】:

正如你所说,它不会进入 imagePicker 方法,看来你错过了正确工作 imagePicker 方法的委托

添加UIImagePickerControllerDelegate并创建一个变量来分配UIImagePickerControllervar imagePicker = UIImagePickerController() 并设置imagePicker.delegate = self

【讨论】:

  • 我已经在整个班级的顶部有 UIImagePickerControllerDelegate 了。还设置了一个变量来分配UIImagePickerController,看代码就行了。
【解决方案2】:

你应该这样使用。您不能在操作中使用函数。

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

        }
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let image1 = info[UIImagePickerController.InfoKey.originalImage] as! UIImage

        addPictureOutlet.setImage(image1, for: .normal)
    }

    @IBAction func addPicture(_ sender: UIButton) {
        chooseSource()
    }

【讨论】:

  • 谢谢,但我有多个 UIButtons 需要此功能,我无法使用 sender,我还想节省一些多余的代码行。
  • 编辑:当您必须在拍摄或使用图片之间进行选择时,我也被您的代码卡住了,按钮不起作用,仍然没有错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多