将NSImage导出/导入为ICO

时间:2010-07-10 16:34:44

标签: cocoa icons save nsimage

我想保存并阅读ICO文件。它的NSImage或NSBitmapImageRep或其他任何东西都无关紧要。它只需要完成。怎么能实现呢?

1 个答案:

答案 0 :(得分:1)

我为自己找到了它。这是我的代码:

NSImage *o = [[NSImage alloc] initWithContentsOfFile:path];
CGImageDestinationRef dest = CGImageDestinationCreateWithURL(
                                                             (CFURLRef)[NSURL fileURLWithPath:newPath], 
                                                             (CFStringRef)@"com.microsoft.ico", 
                                                             o.representations.count,
                                                             NULL);
for (NSBitmapImageRep *rep in o.representations) {
    CGImageRef ref = rep.CGImage;
    CGImageDestinationAddImage(dest, 
                               ref, 
                               NULL);
}


CGImageDestinationFinalize(dest);