Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As of iOS 4.0, the iOS SDK includes the <a href="http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGImageSource/Reference/reference.html"><code>CGImageSource...</code> functions</a> (in the ImageIO framework). It's a very flexible API to query metadata without loading the image into memory. Getting the pixel dimensions of an image should work like this (make sure to include the ImageIO.framework in your target):</p> <pre><code>#import &lt;ImageIO/ImageIO.h&gt; NSURL *imageFileURL = [NSURL fileURLWithPath:...]; CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL); if (imageSource == NULL) { // Error loading image ... return; } CGFloat width = 0.0f, height = 0.0f; CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL); CFRelease(imageSource); if (imageProperties != NULL) { CFNumberRef widthNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth); if (widthNum != NULL) { CFNumberGetValue(widthNum, kCFNumberCGFloatType, &amp;width); } CFNumberRef heightNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight); if (heightNum != NULL) { CFNumberGetValue(heightNum, kCFNumberCGFloatType, &amp;height); } // Check orientation and flip size if required CFNumberRef orientationNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyOrientation); if (orientationNum != NULL) { int orientation; CFNumberGetValue(orientationNum, kCFNumberIntType, &amp;orientation); if (orientation &gt; 4) { CGFloat temp = width; width = height; height = temp; } } CFRelease(imageProperties); } NSLog(@"Image dimensions: %.0f x %.0f px", width, height); </code></pre> <p>(adapted from "Programming with Quartz" by Gelphman and Laden, listing 9.5, page 228)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload