Search Your Question

How to save image in NSUserDefault?

Ans : 

NSUserDefault support format NSData for save. So convert image into NSData and then save.

Save image in NSUserDefault :

    let defaults = NSUserDefaults.standardUserDefaults()
   var imgData = UIImageJPEGRepresentation(image, 1)

   defaults.setObject(imgData, forKey: "image")


Get image from NSUserDefault :

    let defaults = NSUserDefaults.standardUserDefaults()
      if let imgData = defaults.objectForKey("image") as? NSData
      {
         if let image = UIImage(data: imgData)
           {
               self.imageView.image = image
           }

      }

No comments:

Post a Comment

Thanks