To save a UIImage to user defaults in Swift, we need to convert the UIImage to Data using UIImagePNGRepresentation or UIImageJPEGRepresentation method and then save the Data to user defaults.
It is also possible to achieve this using the Codable protocol or archiving approach which allows for storing complex objects like UIImage into user defaults.
Here is the sample code using UIImagePNGRepresentation method:
main.swift276 chars9 linesAnd here is the sample code using Codable approach:
main.swift312 chars10 linesAnd finally, here is the sample code using archiving approach:
main.swift274 chars9 linesgistlibby LogSnag