iOS Dev Nugget 43 Accessing Image Properties Without Loading Entire Image Into Memory


Need to run a code review on your codebase? Hire me

Sometimes you want to retrieve an image file's properties but don't need to display (and hence read) the image itself. This is particularly important if you need to retrieve the properties of many image files, as reading the whole file into memory takes time and resources. You can access the image properties using the Image I/O framework:

#import <ImageIO/ImageIO.h>
//Some path
NSString* path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"back-btn.png"];
NSURL* imageFileURL = [NSURL fileURLWithPath:path];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)imageFileURL, NULL);
if (imageSource) {
    NSDictionary* options = @{(NSString*)kCGImageSourceShouldCache:@NO};
    CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (__bridge CFDictionaryRef)options);
    if (imageProperties) {
        NSLog( @"properties: %@", imageProperties);
} else {
    NSLog(@" Error loading image");

Courtesy of Accessing Image Properties Without Loading the Image into Memory (slightly modified, modernized)

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No


Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter


View archives of past issues