georgeOfTheJungle
georgeOfTheJungle

Reputation: 113

In Xcode, how do I extract .png image from an .icns file programmatically?

A beginner in iOS7 and Xcode. If I understand well, an .icns file is a container and not an image. It contains different sizes with different extensions (e.g. png, jpg) of the icons and perhaps other stuff. Right or wrong?

If so, is there a way of extracting a 512x512.png version of icon from this file? I was told I have to use NSdata to get hold of the contents of .icns file and then substring it to get the image data. But what I don't know is where does the block of code for 512x512.png starts where does it end?

I hope I am formulating my question correctly. I am new at this.

Thank you for your help

Upvotes: 1

Views: 1094

Answers (1)

Codo
Codo

Reputation: 78825

I don't have any code ready (though you might want to look at libicns).

Basically, you have to read the file contents sequentially. First you read the header (8 bytes). Then after the header, you read icon by icon. Each icon starts with 8 byte header containing the type and length. You read the type and length and if the type doesn't match, you skip the icon and proceed with the next one.

Once you have found the desired icon, you can extract it. It starts right after the length field and is length - 8 bytes long (the length includes the header which is only needed for the icns file format).

Note that the length is stored with the most significant bit first (big endian) but iOS is little endian. So you need to convert the numbers.

The icon type can either be treated as string of four characters (without a trailing zero) or as a unsigned 4 byte number. Some compilers can treat 'ic09' as an unsigned int constant (note the single quotes vs. the double quotes for a string).

Upvotes: 3

Related Questions