我正在开发一个应用程序,在其中我提示用户用手机拍照然后我把这张照片放到我的imageView
但是我想保存那张图片以便用户使用它重启应用程序。
这是我的代码:
var imagePicker: UIImagePickerController!
//MARK: - Take image
@IBAction func takePhoto(_ sender: UIButton) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
//MARK: - Done image capture here
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker.dismiss(animated: true, completion: nil)
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
使用上面的代码,我会拍照并将其放入我的imageView
,但我不知道如何保存它,例如UserDefaults
答案 0 :(得分:1)
使用此代码 -
保存图片 -
UserDefaults.standard.set(UIImagePNGRepresentation(image), forKey: "image")
检索图片
let imageData: Data? = UserDefaults.standard.object(forKey: "image") as? Data
if let imageData = imageData
{
var image = UIImage(data: imageData)
}