How to save image

前端 未结 3 1062
挽巷
挽巷 2021-01-27 09:27

I want to save image with userdefaults and use it in another view with this code but it shows an error .

UserDefaults.standard.set(img , forKey : \"simg\")
         


        
3条回答
  •  灰色年华
    2021-01-27 09:49

    You can't save a UIImage directly to NSUserDefaults. You could save it as NSData, but it really is not advisable. NSUserDefaults is meant to store small bits of information in key value pairs. If you start throwing images in there, you could overload the space reserved for it. The contents of the NSUserDefaults is written to a plist. I believe any time you access the user defaults, it reads the whole file. If you put large images in there, it will cause a lot of unnecessary I/O.

    If you still decide to do it (not recommended), here is a good post on getting an NSData object from the image.

提交回复
热议问题