0

I am trying to create an app for the Mac OS X that will convert an image type to an icns file. I'm wondering how I can get started on doing so. Any suggestions would be nice!

Thanks,

Kevin

lab12
  • 6,400
  • 21
  • 68
  • 106

3 Answers3

3

Use CGImageSource APIs (e.g., CGImageSourceCreateWithURL, CGImageSourceCreateImageAtIndex) to load each image into a CGImageRef. Then use CGImageDestination APIs (e.g., CGImageDestinationCreateWithURL, CGImageDestinationAddImage, CGImageDestinationFinalize) to combine however many images you have into one icon file. The 3rd parameter of CGImageDestinationCreateWithURL would be kUTTypeAppleICNS.

JWWalker
  • 22,385
  • 6
  • 55
  • 76
  • Thanks! I'll definitely look into those APIs – lab12 Dec 20 '10 at 00:18
  • "`CGImageDestination`, produces buggy ICNS files in pre 10.7 (maybe pre 10.6.8)." http://stackoverflow.com/questions/27808025/how-to-progrmatically-make-mac-os-x-icns-with-10-differnt-images-using-sips-or-o/27809425#comment44029912_27809425 – Noitidart Jan 08 '15 at 05:06
1

1 Create a folder named icon.iconset.
2 Add one or more of the following images to the folder:

icon_16x16.png
icon_16x16@2x.png
icon_32x32.png
icon_32x32@2x.png
icon_128x128.png
icon_128x128@2x.png
icon_256x256.png
icon_256x256@2x.png
icon_512x512.png
icon_512x512@2x.png

3 Run this command and icon.icns will be created.

iconutil -c icns icon.iconset

http://developer.apple.com/library/mac/#documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Optimizing/Optimizing.html#//apple_ref/doc/uid/TP40012302-CH7-SW2

Codler
  • 10,951
  • 6
  • 52
  • 65
0

Save the NSImage which contains the icon as a TIFF file (use NSData* tiff = [image TIFFRepresentation]; to create a NSData with the TIFF file, and then just use [tiff writeToFile:tiffFile atomically:YES]; to save it in some folder), then use NSTask to convert the TIFF file to an ICNS file using tiff2icns.

tiff2icns /Users/Me/Desktop/pic.tiff /Users/Me/Desktop/pic.icns

Now, an example of the complete code (image is a NSImage file the icon, and iconFile is an NSString with the final location of the icns):

    NSString* tiffFile = [NSString stringWithFormat:@"%@.tiff",iconFile];

    NSData* tiff = [image TIFFRepresentation];
    [tiff writeToFile:tiffFile atomically:YES];

    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"/usr/bin/tiff2icns"];

    [task setArguments:[NSArray arrayWithObjects:tiffFile, iconFile, nil]];
    [task launch];
    [task waitUntilExit];

    [[NSFileManager defaultManager] removeItemAtPath:tiffFile error: NULL];

And that's it.

Andrew Rondeau
  • 667
  • 7
  • 18
VitorMM
  • 1,060
  • 8
  • 33