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

时间:2018-11-01 19:07:42

标签: ios imageview swift4.2

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

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

imageview and button

当我单击“拍照”时,它允许我拍照或从图库中选择照片,如代码所示:

@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中,它正在上传我在图像视图中显示的照片,如下所示,而不是我刚刚用手机拍摄的照片。

我该如何实现?

已编辑

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()


}

1 个答案:

答案 0 :(得分:0)

搜索并浏览了教程之后,我终于有了一些工作:

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