My name is Jake Marsh.
I'm a developer, designer, and writer.

Subscribed via Push Notifications. You can also subscribe via RSS or Twitter.

Subscribe via RSS or Twitter.

Subscribe via Push Notifications, RSS or Twitter.

▸ Accessing Image Properties Without Loading the Image

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

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.

Vote on Hacker News