本地保存 UIImageView
Saving UIImageView Locally
现在我可以让用户使用相机成功拍照并将他们的照片上传到图像视图中。但是,如果用户最终 return 离开该视图控制器,则照片将不再位于该图像视图中。无论如何,我是否可以保存照片,以便在拍摄照片并将其放入该图像视图后,直到用户拍摄不同的照片来替换它为止?
import UIKit
class ArsenalViewController: UIViewController,
UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var ball1: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func addPhoto1(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
ball1.contentMode = .scaleToFill
ball1.image = pickedImage
}
picker.dismiss(animated: true, completion: nil)
}
首先,您需要将图像数据保存在某处(即 UserDefaults、Core Data、数据库),然后检索它。 UserDefaults 很简单,试试这个:
//Encoding
let imageData:NSData = UIImageJPEGRepresentation(pickedImage!)! as NSData
//Saved image
UserDefaults.standard.set(imageData, forKey: "savedImage")
//Decode
if let data = UserDefaults.standard.object(forKey: "savedImage") as! NSData{
myImageView.image = UIImage(data: data as Data)
}
图像编码和保存应在更新图像后立即进行,解码应在视图加载其所有元素(viewDidAppear 方法)后进行。
图像编码并保存一次后,您可以随时对其进行解码,即使用户离开 VC 又回来了。
现在我可以让用户使用相机成功拍照并将他们的照片上传到图像视图中。但是,如果用户最终 return 离开该视图控制器,则照片将不再位于该图像视图中。无论如何,我是否可以保存照片,以便在拍摄照片并将其放入该图像视图后,直到用户拍摄不同的照片来替换它为止?
import UIKit
class ArsenalViewController: UIViewController,
UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var ball1: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func addPhoto1(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
ball1.contentMode = .scaleToFill
ball1.image = pickedImage
}
picker.dismiss(animated: true, completion: nil)
}
首先,您需要将图像数据保存在某处(即 UserDefaults、Core Data、数据库),然后检索它。 UserDefaults 很简单,试试这个:
//Encoding
let imageData:NSData = UIImageJPEGRepresentation(pickedImage!)! as NSData
//Saved image
UserDefaults.standard.set(imageData, forKey: "savedImage")
//Decode
if let data = UserDefaults.standard.object(forKey: "savedImage") as! NSData{
myImageView.image = UIImage(data: data as Data)
}
图像编码和保存应在更新图像后立即进行,解码应在视图加载其所有元素(viewDidAppear 方法)后进行。
图像编码并保存一次后,您可以随时对其进行解码,即使用户离开 VC 又回来了。