如何将 Cocoa 文件类型图标写为 16x16 PNG 文件?
如何在 Cocoa 中为文件类型图标编写 16x16 PNG 文件?我之前尝试过使用如下代码片段所示的代码。该代码片段在运行 Mac OS X 10.5 的 PPC 机器上运行,但似乎不再在 Mac OS X 10.6 上运行:写出的 PNG 大小为 512x512,而不是 16x16。
NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode(kGenericFolderIcon)];
[icon setSize: NSMakeSize(16.0,16.0)];
NSBitmapImageRep * bitmapRep = [NSBitmapImageRep imageRepWithData: [icon TIFFRepresentation]];
NSData * data = [bitmapRep representationUsingType: NSPNGFileType properties: nil];
[data writeToFile: @"/tmp/test.png" atomically: NO];
根据我对 NSImage
和 NSImageRep
文档的了解,上述代码之前工作的事实是巧合,因为 setSize:
仅设置图像的“绘图尺寸”不一定与其表示的“物理尺寸”相匹配。
那么获取 16x16 大小的 PNG 文件的正确方法是什么?此外,Mac OS X 上的图标可以包括专门绘制的 16x16 尺寸版本,这不仅仅是“大”图标的缩小版本;当有可用的 PNG 文件时,如何确保将这一特殊版本写入 PNG 文件?
How do you write out a 16x16 PNG file for a file type icon in Cocoa? I tried this before with code like the snippet below. The snippet worked on a PPC machine with Mac OS X 10.5, but no longer seems to work on Mac OS X 10.6: the PNG that's written out has size 512x512, rather than 16x16.
NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode(kGenericFolderIcon)];
[icon setSize: NSMakeSize(16.0,16.0)];
NSBitmapImageRep * bitmapRep = [NSBitmapImageRep imageRepWithData: [icon TIFFRepresentation]];
NSData * data = [bitmapRep representationUsingType: NSPNGFileType properties: nil];
[data writeToFile: @"/tmp/test.png" atomically: NO];
From what I understand from the documentation on NSImage
and NSImageRep
, the fact that the above code worked before was coincidence, as setSize:
only sets the "drawing size" of the image which doesn't necessarily match the "physical size" of its representation(s).
So what is the correct way to get the 16x16 size PNG file? Also, icons on Mac OS X can include a specially-drawn version for the 16x16 size, which is not just a scaled-down version of the "big" icon; how do you make sure this special version is written out to the PNG file when one is available?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Nikolai 是对的,您应该首先检查图像是否具有 16x16 表示形式,但要将图像大小简单地调整为 16 x 16,您可以这样做:
Nikolai is right, you should check to see if the image has a 16x16 representation first, but to simply resize an image to 16 x 16 you would do this:
如果您确定 NSImage 中存在 16x16 大小的图标版本,您可以查看表示形式 (
-[NSImagerepresentations]
),然后通过查看来选择您感兴趣的图标大小(-[NSImageRep PixelsWide]
和pixelsHigh
)。然后您可以将表示形式写入磁盘。更好的方法是创建一个 16x16 位图上下文,将图像绘制到该上下文中,然后保存上下文内容。如果原始图标不包含 16x16 表示,此方法也适用。
If you are sure that there's a 16x16 sized version of the icon present in the NSImage, you could look through the representations (
-[NSImage representations]
) and select the one you're interested in by looking at the size (-[NSImageRep pixelsWide]
andpixelsHigh
). You could then write the representation to disk.A better way would be to create a 16x16 bitmap context, draw the image into that context and then save the contexts contents. This way also works if the original icon does not contain a 16x16 representation.