将照片保存到 Swift 中的图像 4.2

Saving photo to image in Swift 4.2

我在显示刚刚拍摄或选择的照片到图像视图时遇到问题。

下面是左侧的 imagview 和右侧的 licencePhoto 按钮的图片。

当我点击拍照时,它允许我从我的图库中拍照或 select,如代码所示:

@IBAction func seclectOrTakePhoto(_ sender: Any) {

    pickerController.delegate = self
    pickerController.allowsEditing = true

    let alertController = UIAlertController(title: "Add a Picture", message: "Choose From", preferredStyle: .actionSheet)

    let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
        self.pickerController.sourceType = .camera
        self.present(self.pickerController, animated: true, completion: nil)

    }

    let photosLibraryAction = UIAlertAction(title: "Photos Library", style: .default) { (action) in
        self.pickerController.sourceType = .photoLibrary
        self.present(self.pickerController, animated: true, completion: nil)

    }

    let savedPhotosAction = UIAlertAction(title: "Saved Photos Album", style: .default) { (action) in
        self.pickerController.sourceType = .savedPhotosAlbum
        self.present(self.pickerController, animated: true, completion: nil)

    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)

    alertController.addAction(cameraAction)
    alertController.addAction(photosLibraryAction)
    alertController.addAction(savedPhotosAction)
    alertController.addAction(cancelAction)


    present(alertController, animated: true, completion: nil)

}

但是,它不会将照片显示到 licensePhoto 图像中,并且在 Firebase 中,它正在上传我在图像视图中显示的照片,如下所示,而不是我刚刚使用 phone 拍摄的照片。

我怎样才能做到这一点?

已编辑

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    self.licensePhoto.image = image
    self.dismiss(animated: true, completion: nil)


    // any time the photo changes, check the button status
    updateSignupButtonStatus()


}

在搜索和浏览教程之后,我终于找到了可以工作的东西:

@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    licensePhoto.contentMode = .scaleAspectFit
    licensePhoto.image = chosenImage
    pickerController.dismiss(animated: true, completion: nil)
}