更改UIImageView图像并使用NSUserDefaults保存

时间:2016-02-15 04:18:25

标签: ios swift uiimageview swift2 nsuserdefaults

我一直致力于一个允许用户只需按一下按钮即可切换UIImageView图像的应用程序。更具体地说,我保存了两个图像,当您按下按钮时,UIImageView图像会发生变化。对于那件事我没有任何疑问。问题是,当我关闭应用程序时,图像重置为我最初设置为初始UIImageView图像的图像。

我想知道,有没有办法保留上次选择的图像?换句话说:

  • 当应用程序启动时,图像将设置为image1。
  • 用户按下按钮,图像更改为image2
  • 用户关闭应用并重新打开,除非用户再次点击按钮,否则image2仍然是背景

这是我到目前为止的代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var backgroundImage: UIImageView!


let lightBG = UIImage(named: "bg1.jpg")
let darkBG = UIImage(named: "bg2.jpg")

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 changeBG(sender: AnyObject) {
    if(backgroundImage.image == lightBG){
        backgroundImage.image = darkBG
    }
    else{
        backgroundImage.image = lightBG
    }
}

}

4 个答案:

答案 0 :(得分:2)

<强>步骤1

 override func viewDidLoad() {
   super.viewDidLoad()

     // Initially Check your `NSUserDefaults` key is empty or not 

       if NSUserDefaults.standardUserDefaults().objectForKey("SaveImage") != nil  
   {
    // If it is not empty , load the `NSUserDefaults` Image
   backgroundImage.image = UIImage(named: NSUserDefaults.standardUserDefaults().objectForKey("SaveImage"))

   }
else
 {
  // if above condition is not statisfy load the normal Image
  backgroundImage.image = lightBG
  }
}


 @IBAction func changeBG(sender: AnyObject) {

    if(backgroundImage.image == lightBG){
        backgroundImage.image = darkBG
           }
    else{
        backgroundImage.image = lightBG

    }

// On your button click , load the last selected Image , thats all
 NSUserDefaults.standardUserDefaults().setObject(backgroundImage.image, forKey: "SaveImage")


}

答案 1 :(得分:2)

您可以通过多种方式保存所选图像。一种是在视图退出时或用户更改图像时以用户默认值保存图像名称。

以下是用户更改图像时如何保存它:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var backgroundImage: UIImageView!

let kBGImageNameKey = "bgImageName"

let lightBG = UIImage(named: "bg1.jpg")
let darkBG = UIImage(named: "bg2.jpg")

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let defaults = NSUserDefaults.standardUserDefaults()
    if let name = defaults.stringForKey(kBGImageNameKey) {
        if (name == "bg1.jpg") {
            backgroundImage.image = lightBG
        } else {
            backgroundImage.image = darkBG
        }
    } 
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func changeBG(sender: AnyObject) {
    let defaults = NSUserDefaults.standardUserDefaults()

    if(backgroundImage.image == lightBG){
        backgroundImage.image = darkBG
        defaults.setObject("bg2.jpg", forKey: kBGImageNameKey)
    }
    else{
        backgroundImage.image = lightBG
        defaults.setObject("bg1.jpg", forKey: kBGImageNameKey)
    }
}

}

这只是一种方法,只是为了让你朝着这个方向前进。

答案 2 :(得分:1)

@IBAction func changeBG(sender: AnyObject) {

    if(backgroundImage.image == lightBG){
        backgroundImage.image = darkBG
        NSUserDefaults.standardUserDefaults().setObject("bg2.jpg", forKey: "BACKGROUND_IMAGE_NAME")
    }
    else{
        backgroundImage.image = lightBG
        NSUserDefaults.standardUserDefaults().setObject("bg1.jpg", forKey: "BACKGROUND_IMAGE_NAME")
    }

  NSUserDefaults.standardUserDefaults().synchronize()
}

override func viewDidAppear(animated: Bool) {
   super.viewDidAppear(animated)

   var imageName: String = NSUserDefaults.standardUserDefaults().objectForKey("BACKGROUND_IMAGE_NAME")
   if imageName.characters.count != 0 {
      let imgBackGround = UIImage(named: imageName)
      backgroundImage.image = imgBackGround
   }
}

答案 3 :(得分:1)

更好的选择是在Boolean而不是整个图片中保存NSUserDefaults

@IBAction func changeBG(sender: AnyObject) {

    if(backgroundImage.image == lightBG){
        backgroundImage.image = darkBG
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "IMAGE_DARK_SET")
        NSUserDefaults.standardUserDefaults().setBool(false, forKey: "IMAGE_LIGHT_SET")
    }
    else{
        backgroundImage.image = lightBG
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "IMAGE_LIGHT_SET")
        NSUserDefaults.standardUserDefaults().setBool(false, forKey: "IMAGE_DARK_SET")
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    if NSUserDefaults.standardUserDefaults().boolForKey("IMAGE_LIGHT_SET"){

        backgroundImage.image = lightBG

    }else if NSUserDefaults.standardUserDefaults().boolForKey("IMAGE_DARK_SET"){

        backgroundImage.image = darkBG
    }
}

希望这有帮助!