3

好的,这就是我想要的:

  • NSImage一些
  • 将它们添加到ICNS文件中
  • 保存

这是我到目前为止所做的(纯粹作为测试):

- (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);
}

但是,它仍然不断抛出错误:

ImageIO:CGImageDestinationFinalize 图像目的地没有足够的图像

我的代码有什么问题?


我已经看过下面的答案,但仍然没有:

4

1 回答 1

4

您可以使用IconFamily

IconFamily 是 Mac OS X Carbon API 的“图标系列”数据类型的 Cocoa/Objective-C 包装器。其主要目的是使 Cocoa 应用程序能够轻松地从 NSImage 实例创建自定义文件图标,从而利用 Mac OS X 的高分辨率 RGBA“缩略图”图标格式来提供文件内容的丰富详细的缩略图预览。

NSImage *mImage = [[NSImage alloc] initWithContentsOfFile:@"/Users/Username/Desktop/WhiteTiger.jpg"];
IconFamily *fam = [IconFamily iconFamilyWithThumbnailsOfImage:mImage];    
[fam writeToFile:@"/Users/Username/Desktop/WhiteTiger.icns"];
于 2012-09-07T09:57:39.207 回答