NSUserDefaultsにUIImageの配列を保存できません
SecondViewControllerで配列にUIImageを保存したいです。
まず、SecondViewControllerで写真を配列[imageFile]に保存します。
import UIKit
var imageFile = [UIImage]()
class SecondViewController: UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
let pho = UIImagePickerController()
pho.delegate = self
pho.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(pho, animated: true, completion: nil)
}
}
var original:UIImage!
@IBOutlet weak var imageView: UIImageView!
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if info[UIImagePickerControllerOriginalImage] != nil{
original = info[UIImagePickerControllerOriginalImage] as? UIImage
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
picker.dismissViewControllerAnimated(true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func save(sender: UIButton) {
imageFile.append(original)
NSUserDefaults.standardUserDefaults().setObject(imageFile, forKey: "Key")
}
}
次にFirstViewControllerで保存したものを受け取りたいのですが
SecondViewControllerのsaveボタンを押したらエラーが出ます。
他のプログラムでも同じことをしましたができませんでした。
文字の配列では、できました。
NSUserDefaultsではUIImageの配列は扱えないのでしょうか?
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if NSUserDefaults.standardUserDefaults().arrayForKey("Key") != nil{
imageFile = NSUserDefaults.standardUserDefaults().arrayForKey("Key") as! [UIImage]
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var label: UILabel!
@IBAction func load(sender: UIButton) {
label.text! = String(imageFile.count)
}
}