Can I save an image using codable protocol, if so, how?

I want to save an image for persistence through adopting Codable protocol. Kindly help to do so.

Replies

The most straightforward way to do this would be to write your own implementations of the

Codable
methods that convert between the image and data (using
UIImagePNGRepresentation
or
UIImageJPEGRepresentation
to go one way and
UIImage(data:)
to go the other).

However, you should think carefully before doing down that path. Serialised images tend to be large and many of the serialisation formats associated with

Codable
don’t deal well with very large blobs of data. You may be better off saving the image in a separate file and then have your
Codable
implementation reference that.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"