OK this is what I want :
- Take some
NSImage
s - Add them to an
ICNS
file - Save it
This is what I've 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, it still keeps throwing an error :
ImageIO: CGImageDestinationFinalize image destination does not have enough images
What's wrong with my code?
I've had a look at the answers below, but still nothing :
- Save CGImageRef to PNG file errors? (ARC Caused?)
- How exactly to make a CGImageRef from an image on disk