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 this 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:

Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at and

Add your voice!