Swift - saving and retrieving images from userDefaults

前端 未结 2 2007
野性不改
野性不改 2021-01-26 00:22

I\'m trying to save images retrieved from Parse.com like this:

                    let userImageFile = object[\"Image\"] as PFFile
                    userImageF         


        
相关标签:
2条回答
  • 2021-01-26 00:34

    It seems like you do not call defaults.synchronize() so it's not written to the defaults file.

    0 讨论(0)
  • 2021-01-26 00:37

    Swift 3

    Hey, try this beautiful code here:

    • Convert your UIImage to Data.

      PNG:

      yourDataImagePNG = UIImagePNGRepresentation(yourUIImageHere)
    

    JPEG :

     yourDataImageJPG = UIImage(data: yourUIImageHere,scale:1.0)
    
    • Save in UserDefaults.
        UserDefaults().set(yourDataImagePNG, forKey: "image")
    
    • Recover from:
        UserDefaults.standard.object(forKey: "image") as! Data
    

    I hope to help!

    0 讨论(0)
提交回复
热议问题