How do I get the icon of the user's Mac?

拜拜、爱过 提交于 2019-11-29 21:57:32
[NSImage imageNamed: NSImageNameComputer]

This will return the icon of the current computer

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!