如何在 Mac OS X 中获取存储设备的图标?
我使用 IOServiceGetMatchingServices 找到了我的设备,并获得了如下所示的属性字典:
kernResult = IORegistryEntryCreateCFProperties(nextMedia,
(CFMutableDictionaryRef *)&props,
kCFAllocatorDefault, 0);
从该字典中,我可以提取图标的信息:
NSString *bId = [props valueForKeyPath:@"IOMediaIcon.CFBundleIdentifier"];
NSString *rFile = [props valueForKeyPath:@"IOMediaIcon.IOBundleResourceFile"];
这两个给了我这个(作为示例):
com.apple.iokit.IOStorageFamily (Bundle identifier) Internal.icns (Resource File)
我尝试提取使用此方法的图标:
NSBundle *bundleWithIcon = [NSBundle bundleWithIdentifier:bId];
NSString *iconPath = [bundleWithIcon pathForResource:rFile ofType:nil];
但是bundleWithIcon
是nil
。
这是否是获取图标的正确方法?
我想我必须以某种方式加载捆绑包才能使用 bundleWithIdentifier
加载它,我该怎么做?
PS:还有另一个问题(我认为)试图问同样的事情,但只是问对于捆绑包,如果这是正确的方法则不是。
I've found my devices using IOServiceGetMatchingServices
and got the property dictionary like this:
kernResult = IORegistryEntryCreateCFProperties(nextMedia,
(CFMutableDictionaryRef *)&props,
kCFAllocatorDefault, 0);
From that dictionary I can extract the informations for the icons:
NSString *bId = [props valueForKeyPath:@"IOMediaIcon.CFBundleIdentifier"];
NSString *rFile = [props valueForKeyPath:@"IOMediaIcon.IOBundleResourceFile"];
Those two give me this (as an example):
com.apple.iokit.IOStorageFamily (Bundle identifier) Internal.icns (Resource File)
I tried to extract the icon using this method:
NSBundle *bundleWithIcon = [NSBundle bundleWithIdentifier:bId];
NSString *iconPath = [bundleWithIcon pathForResource:rFile ofType:nil];
But bundleWithIcon
is nil
.
Is this even the correct method to get the icon?
I think I have to somehow load the bundle to be able to load it with bundleWithIdentifier
, how can I do this?
PS: There's another question which (I think) tries to ask the same thing, but only asks for bundles, not if this is the correct way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 NSWorkspace。
初始图像为 32x32,但它具有其他尺寸的表示形式,并将相应缩放
You could use NSWorkspace.
The initial image is 32x32, but it has representations for the other sizes and will scale accordingly
就在最近Andrew Myrick 回答了类似的问题达尔文开发邮件列表:
对我来说,这在 Mac OS X 10.5 上运行得非常好。
Just recently Andrew Myrick answered a similar question on the darwin-dev mailing list:
For me this worked really well on Mac OS X 10.5.
这可能对你有帮助。 (或者也许不是)......
在使用“ioreg”命令时,我遇到了一些让我想起你的问题的东西,所以我将发布它:
尝试发出以下命令:
这将产生一团糟的输出看起来像这样:
这一切都让我相信(因此这里盲目地建议您进行调查)如果您遍历与“IOMedia”匹配的 io 注册表树,您可以获得包含键为“IOMediaIcon”的条目的属性字典,它本身似乎是一个通知您包标识符和资源文件名的集合。
并不是说这很容易...但是请查看 FireWire SDK 以获取您可能需要的所有示例代码...
无论如何,它可能比硬编码预填充路径“更好”(这可能在未来的操作系统版本中消失)。
|K<
this may help you. (or maybe not)...
in playing around with the 'ioreg' command i came across something that reminded me of your question, and so I'll post it:
try issuing the following command:
which will yield a big mess of output which looks something like this:
this all leads me to believe (and thus here blindly recommend that you investigate) that if you traverse the io registry tree matching against 'IOMedia' you can get property dictionaries that will contain an entry keyed "IOMediaIcon," which itself appears to be a collection informing you of a bundle identifier and a resource file name.
not to say that this is easy... but look into the FireWire SDK for all the example code you may need...
in any case it's probably "better" than hard-coding pre-filled paths (which might disappear in future OS releases).
|K<
bundleWithIdentifier:
要求您已经为具有该标识符的包创建了一个 NSBundle 实例; 它只查找以前创建的实例。 因此,您需要在文件系统中找到该包并通过路径名实例化它。 幸运的是,您似乎已经有了有关该问题的链接。bundleWithIdentifier:
requires you to have already created an NSBundle instance for a bundle with that identifier; it only looks up a previously-created instance. Therefore, you'll need to find the bundle in the file-system and instantiate it by pathname. Fortunately, you seem to already have the link to the question about that.没有自定义图标的卷将显示为操作系统的通用图标之一,您已在此处硬编码了该图标的路径。 具有自定义图标的卷将将该图标存储在其文件系统上,如果未安装该图标,则查找它就完全是您的工作了。
A volume without a custom icon is going to be displayed with one of the OS's generic icons which you've hard-coded paths to here. A volume with a custom icon is going to store that icon on its filesystem, and if it's not mounted, finding it becomes entirely your job.