Image File Formats

Starting in iOS 4, the Image I/O framework provides a simple, unified way to open image files (from disk or downloaded from the network, as described in Chapter 37), to save image files, to convert between image file formats, and to read metadata from standard image file formats, including EXIF and GPS information from a digital camera. You’ll need to link to ImageIO.framework and import <ImageIO/ImageIO.h>.

Obviously, such features were not entirely missing before iOS 4. UIImage can read the data from most standard image formats, and you can convert formats with functions such as UIImageJPEGRepresentation and UIImagePNGRepresentation. But you could not, for example, save an image as TIFF without the Image I/O framework.

The Image I/O framework introduces the notion of an image source (CGImageSourceRef). This can be created from the URL of a file on disk or from NSData (actually CFDataRef, to which NSData is toll-free bridged) obtained or generated in some way. You can use this to obtain a CGImage of the source’s image (or, if the source format contains multiple images, a particular image). But you can also obtain metadata from the source without transforming the source into a CGImage, thus conserving memory. For example:

NSURL* url = [[NSBundle mainBundle] URLForResource:@"colson" withExtension:@"jpg"]; CGImageSourceRef src = CGImageSourceCreateWithURL((CFURLRef)url, NULL); NSDictionary* result = (id)CGImageSourceCopyPropertiesAtIndex(src, 0, NULL); // ... do something ...

Get Programming iOS 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.