How to create an ICNS icon programmatically?

OK, this is what I want:

  • Take some NSImage s
  • Add them to the ICNS file
  • Save

This is what I have done so far (purely as a test):

 - (CGImageRef)refFromImage:(NSImage*)img { CGImageSourceRef source; source = CGImageSourceCreateWithData((CFDataRef)[img TIFFRepresentation], NULL); CGImageRef maskRef = CGImageSourceCreateImageAtIndex(source, 0, NULL); return maskRef; } - (void)awakeFromNib { NSImage* img1 = [NSImage imageNamed:@"image1"]; NSImage* img2 = [NSImage imageNamed:@"image2"]; NSLog(@"%@",img1); CGImageRef i1 = [self refFromImage:img1]; CGImageRef i2 = [self refFromImage:img2]; NSURL *fileURL = [NSURL fileURLWithPath:[@"~/Documents/final.icns" stringByExpandingTildeInPath]]; CGImageDestinationRef dr = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeAppleICNS , 1, NULL); CGImageDestinationAddImage(dr, i1, NULL); CGImageDestinationAddImage(dr, i2, NULL); /* Even tried adding 'multiple' times CGImageDestinationAddImage(dr, i1, NULL); CGImageDestinationAddImage(dr, i2, NULL); CGImageDestinationAddImage(dr, i1, NULL); CGImageDestinationAddImage(dr, i2, NULL); */ CGImageDestinationFinalize(dr); CFRelease(dr); } 

But he still continues to throw an error:

ImageIO: CGImageDestinationFinalize image destination does not have enough images

What is wrong with my code?


I looked at the answers below, but still nothing:

+3
source share
1 answer

You can use IconFamily .

IconFamily is the Cocoa / Objective-C wrapper for Mac OS X Carbon API Family Icons data type. Its main goal is to enable Cocoa applications to create custom file icons from NSImage instances and thus use high-resolution RGBA Mac OS X thumbnail icon formats to provide detailed thumbnail previews of file contents.

 NSImage *mImage = [[NSImage alloc] initWithContentsOfFile:@"/Users/Username/Desktop/WhiteTiger.jpg"]; IconFamily *fam = [IconFamily iconFamilyWithThumbnailsOfImage:mImage]; [fam writeToFile:@"/Users/Username/Desktop/WhiteTiger.icns"]; 
+4
source

All Articles