我发现我的设备使用IOServiceGetMatchingServices
并获得了这样的属性字典:
kernResult = IORegistryEntryCreateCFProperties(nextMedia,
(CFMutableDictionaryRef *)&props,
kCFAllocatorDefault, 0);
Run Code Online (Sandbox Code Playgroud)
从那个字典我可以提取图标的信息:
NSString *bId = [props valueForKeyPath:@"IOMediaIcon.CFBundleIdentifier"];
NSString *rFile = [props valueForKeyPath:@"IOMediaIcon.IOBundleResourceFile"];
Run Code Online (Sandbox Code Playgroud)
这两个人给我这个(作为一个例子):
com.apple.iokit.IOStorageFamily (Bundle identifier) Internal.icns (Resource File)
我尝试使用此方法提取图标:
NSBundle *bundleWithIcon = [NSBundle bundleWithIdentifier:bId];
NSString *iconPath = [bundleWithIcon pathForResource:rFile ofType:nil];
Run Code Online (Sandbox Code Playgroud)
不过bundleWithIcon
是nil
.
这甚至是获取图标的正确方法吗?
我想我必须以某种方式加载捆绑包才能加载它bundleWithIdentifier
,我该怎么做?
PS:还有一个问题(我认为)试图提出同样的问题,但只要求捆绑,而不是这是正确的方法.
您可以使用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);
Run Code Online (Sandbox Code Playgroud)