gpt4 book ai didi

objective-c - 如何以编程方式创建 ICNS 图标?

转载 作者:搜寻专家 更新时间:2023-10-30 20:06:16 24 4
gpt4 key购买 nike

好的,这就是我想要的:

  • 取一些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 image destination does not have enough images

我的代码有什么问题?


我已经看过下面的答案,但仍然一无所获:

最佳答案

您可以使用 IconFamily .

IconFamily is a Cocoa/Objective-C wrapper for the Mac OS X Carbon API's "icon family" data type. Its main purpose is to enable Cocoa applications to easily create custom file icons from NSImage instances, and thus take advantage of Mac OS X's high-resolution RGBA "thumbnail" icon formats to provide richly detailed thumbnail previews of the files' contents.

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

关于objective-c - 如何以编程方式创建 ICNS 图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12315162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com