繁体   English   中英

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

[英]Saving photo to image in Swift 4.2

我在显示刚拍摄或选择的照片时出现问题。

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

imageview和按钮

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

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


}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM