This post is from 2011, about 15 years ago. Some of it may be out of date.

▸ Accessing Image Properties Without Loading the Image

1 min readiOS Development

Jake Marsh

I see this type of stuff a lot in other people’s code:

Objective-C
UIImage *image = [UIImage imageWithContentsOfFile:@"lolcats.jpg"];
CGSize imageSize = image.size;

It’s certainly not wrong, but you are incurring a huge performance and memory usage hit just to get the width and height of an image. Enter the CGImageSource... methods.

As of iOS 4, the SDK includes a better solution in the form of the CGImageSource... set of functions, which have been available on the Mac since forever. These functions allow you to access certain image metadata without having to load the actual pixel data into memory.

Ole Begemann has put together a great article explaining the ways around such performance hits, and how you can get pretty much any metadata you could want about an image, without incurring a performance or memory usage hit.

Link: oleb.net/blog/2011/09/accessing-image-properties-without-loading-the-image-into-memory