通过NSUserDefaults保存图像

时间:2014-12-28 17:19:10

标签: ios iphone swift nsuserdefaults

我正在尝试保存一个简单的功能,用户单击按钮并且图像发生变化,我希望图像在用户单击按钮时保持不变。例如,如果用户单击该按钮并且imageview名为hello,则当应用关闭并从后台删除时,它会更改为名为goodbye的图像视图,当用户重新打开应用时goodbye将显示1}}图像。

我需要做些什么才能保存?

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var button: UIImageView!

@IBAction func addButton(sender: AnyObject) {
    button.image = UIImage(named: "testButtonWin")
}
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.
    NSUserDefaults.standardUserDefaults().setObject(button, forKey: "button")
    NSUserDefaults.standardUserDefaults().synchronize()
    //println(NSUserDefaults.standardUserDefaults().objectForKey("button")!)
}


}

0 个答案:

没有答案