gpt4 book ai didi

cocoa - 如何在 Mac OS X 中获取存储设备的图标?

转载 作者:行者123 更新时间:2023-12-03 16:16:25 25 4
gpt4 key购买 nike

我使用 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)

I tried to extract the icon using this method:

NSBundle *bundleWithIcon = [NSBundle bundleWithIdentifier:bId];
NSString *iconPath = [bundleWithIcon pathForResource:rFile ofType:nil];

但是bundleWithIconnil

这是否是获取图标的正确方法?

我想我必须以某种方式加载 bundle 才能使用 bundleWithIdentifier 加载它,我该怎么做?

PS:There's another question (我认为)试图问同样的事情,但只要求 bundle ,如果这是正确的方法则不要求。

最佳答案

您可以使用 NSWorkspace。
初始图像为 32x32,但它具有其他尺寸的表示形式,并将相应缩放

NSWorkspace * ws = [NSWorkspace sharedWorkspace];
NSImage * icon = [ws iconForFile:@"/Volumes/Whatever"];
NSLog(@"%@", [icon representations]); // see what sizes the icon has
icon.size = NSMakeSize(512, 512);

关于cocoa - 如何在 Mac OS X 中获取存储设备的图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/580002/

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