1
在我製作的應用程序中,用戶將選擇一張照片,然後它會出現在imageview中。我如何保存他們在image.xcassets文件夾中選擇的圖像,因此當我重新啓動應用程序時,圖像仍然存在?如何保存用戶用swift選擇的圖像?
下面是我使用的代碼...
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Action(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
let imagePicker:UIImagePickerController = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
self.dismissViewControllerAnimated(true, completion: nil)
self.imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
//how do i save the image in the folder?
}
}
一個可能的解決方案是創建一個CoreData對象做,並保存圖像出現作爲base64編碼的字符串。這樣下次打開應用程序時,您可以從CoreData獲取圖像並在重新啓動時顯示它。請參閱http://stackoverflow.com/a/11251478/1457357瞭解使用base64進行編碼和解碼的最佳解釋。 – 2015-04-01 15:00:34