如何获取用户 Mac 的图标?

发布于 2024-08-04 03:01:15 字数 400 浏览 6 评论 0原文

使用 Objective-C 和 Cocoa,有谁知道如何获取用户计算机的图标(显示在 Finder 中“设备”和“网络”下的图标)?不是硬盘图标,而是用户设备的实际图标。它的范围从 MacBook 图标到 Mac Pro 图标再到 Windows 蓝屏死机监视器图标。

我已经尝试过以下方法:

NSImage *icon = [[NSWorkspace sharedWorkspace] 
                  iconForFileType: NSFileTypeForHFSTypeCode(kComputerIcon)];

但显然,它总是返回相同的图标。我还尝试了 iconForFile: 方法,但我不知道用作参数的文件路径。有人能指出我正确的方向吗?

Using Objective-C and Cocoa, does anyone know how to get the icon for a user's computer (the one that shows under "Devices" and "Network" in Finder)? Not the harddisk icon, the actual one for a user's device. It ranges from a MacBook icon to the Mac Pro icon to a Windows blue screen of death monitor icon.

I've tried stuff along the following lines:

NSImage *icon = [[NSWorkspace sharedWorkspace] 
                  iconForFileType: NSFileTypeForHFSTypeCode(kComputerIcon)];

But that just returns the same icon all the time, obviously. I've also tried the iconForFile: method but I don't know of a file path to use as the parameter. Can anybody point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛溺水了 2024-08-11 03:01:15
[NSImage imageNamed: NSImageNameComputer]

这将返回当前计算机的图标

[NSImage imageNamed: NSImageNameComputer]

This will return the icon of the current computer

风尘浪孓 2024-08-11 03:01:15

另一个查找图标的地方:

/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources

您可以使用其中的文件创建 NSImage 对象,如下所示:

[[NSImage alloc] initWithContentsOfFile:@"/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/com.apple.macbook-unibody.icns"];

但是,可能不建议像这样对值进行硬编码,因为 Apple 可能会更改图标的位置。有一个名为 IconsCore.h 的文件,其中包含许多其他常量值,例如“kToolbarDesktopFolderIcon”,可以按如下方式使用:

[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode(kToolbarDesktopFolderIcon)];

不过,我相信这些常量仅在 Snow Leopard 中有效。

Another place to look for icons:

/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources

You can create NSImage objects with the files in there like this:

[[NSImage alloc] initWithContentsOfFile:@"/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/com.apple.macbook-unibody.icns"];

It's probably not recommended to hard-code the value like that, however, since Apple may change the icons' locations. There is a file called IconsCore.h that contains many other constant values such as 'kToolbarDesktopFolderIcon' which can be used as follows:

[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode(kToolbarDesktopFolderIcon)];

I believe these constants only work in Snow Leopard, though.

辞旧 2024-08-11 03:01:15

如果您正在寻找任何其他系统图标,请查看 Apple 名为“IconCollection”的示例项目。 http://developer.apple.com/mac/library/samplecode/IconCollection /listing5.html

该示例附带了一个 plist 文件,其中包含相当多的系统图标的名称和代码,可以使用以下命令进行访问:

OSType code = UTGetOSTypeFromString((CFStringRef)codeStr);
NSImage *picture = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(code)];

其中codeStr是icons.plist中提供的图标的字符串代码

If you are looking for any other system icons check out Apple's sample project called "IconCollection". http://developer.apple.com/mac/library/samplecode/IconCollection/listing5.html

The sample comes with a plist file that has the names and codes for quite a few system icons that can be accessed using;

OSType code = UTGetOSTypeFromString((CFStringRef)codeStr);
NSImage *picture = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(code)];

where codeStr is the string code for the icon provided in icons.plist

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文