Get Image Data Including Depth, Color Model, DPI and More

Although you can get some basic information if you load an image into a UIImage object, at times it can be helpful to more about an image, for example, the color model (RGB, CMYK, Gray…), resolution in dots-per-inch (DPI), etc.

One way to gather such information is through a CGImageSource object. Often times this object is created using a URL, and once the object is known, you can get image data through a range of C functions (traditional C functions versus methods in an Objective-C object).

The image we’ll use for this tip is shown below:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  // Create URL to file included in the app
  NSURL *url = [[NSBundle mainBundle] URLForResource:@"ImperialStout" withExtension:@"jpg"];
 
  // Reference to an image source
  CGImageSourceRef imageRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
 
  // Get reference to dictionary of image
  CFDictionaryRef imageDict = CGImageSourceCopyPropertiesAtIndex(imageRef, 0, NULL);
 
  // Print image dictionary
  NSLog(@"image dict: %@", imageDict);
 
  // Get references to model, width, height and depth objects
  CFStringRef model = (CFStringRef)CFDictionaryGetValue(imageDict, kCGImagePropertyColorModel);
  CFNumberRef width = (CFNumberRef)CFDictionaryGetValue(imageDict, kCGImagePropertyPixelWidth);
  CFNumberRef height = (CFNumberRef)CFDictionaryGetValue(imageDict, kCGImagePropertyPixelHeight);
  CFNumberRef depth = (CFNumberRef)CFDictionaryGetValue(imageDict, kCGImagePropertyDepth);
 
  // Cast CFStringRef to NSString
  NSLog(@"Image model: %@\n", (NSString *)model);
 
  // Get the values of the number references
  int w = 0, h = 0, d = 0;
  CFNumberGetValue(depth, kCFNumberIntType, &d);
  CFNumberGetValue(width, kCFNumberIntType, &w);
  CFNumberGetValue(height, kCFNumberIntType, &h);
 
  NSLog(@"Image depth: %d\n", d);
  NSLog(@"Image width: %d\n", w);
  NSLog(@"Image height: %d\n", h);
 
  CFRelease(imageDict);
  CFRelease(imageRef);

The code starts by defining a URL to an image, using the same to create a reference to an image source, and building a dictionary of the image data. On line 12, the dictionary contents are printed, the output looks as follows:

2012-05-07 21:52:34.722 Sandbox[972:f803] image dict: {
    ColorModel = RGB;
    DPIHeight = 72;
    DPIWidth = 72;
    Depth = 8;
    Orientation = 1;
    PixelHeight = 304;
    PixelWidth = 200;
    "{Exif}" =     {
        ColorSpace = 1;
        ComponentsConfiguration =         (
            0,
            0,
            0,
            0
        );
        ExifVersion =         (
            2,
            2,
            1
        );
        FlashPixVersion =         (
            1,
            0
        );
        PixelXDimension = 200;
        PixelYDimension = 304;
        SceneCaptureType = 0;
    };
    "{JFIF}" =     {
        DensityUnit = 1;
        JFIFVersion =         (
            1,
            1
        );
        XDensity = 72;
        YDensity = 72;
    };
    "{TIFF}" =     {
        Orientation = 1;
        ResolutionUnit = 2;
        XResolution = 72;
        YResolution = 72;
    };
}

Beginning on line 14, you can see how to retrieve the values for the model, width, height and image depth – lines 20-29 show how to print the values to the console.

The console output follows:

2012-05-07 22:12:07.455 Sandbox[1165:f803] Image model: RGB
2012-05-07 22:12:07.456 Sandbox[1165:f803] Image depth: 8
2012-05-07 22:12:07.457 Sandbox[1165:f803] Image width: 200
2012-05-07 22:12:07.457 Sandbox[1165:f803] Image height: 304

If you know of another way to get the same information, please post a code example.