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\")
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.