How to convert a UIImage into NSData

You can store a UIImage (or an NSImage on Mac) as raw data. This can be useful if you’d like to save it in Core Data.

Here’s how you convert a UIImage into NSData:

To convert NSData back to a UIImage, you can do this:

The UIImage class is not available on Mac OS X, but its counterpart NSImage is almost identical.


Core Data can convert images to and from data on the fly by using an NSValueTransformer.
Here’s one that works for images:

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.