在Objective-C中获取另一个应用程序的图标
如何使用Objective-C从另一个应用程序中获取图标 到目前为止,我已经尝试过了,但是它只返回在Objective-C中获取另一个应用程序的图标,objective-c,macos,Objective C,Macos,如何使用Objective-C从另一个应用程序中获取图标 到目前为止,我已经尝试过了,但是它只返回null: NSURL *path = [NSURL URLWithString:@"/Applications/Calculator.app"]; NSLog(@"%@", path); NSImage *image = (__bridge NSImage *)(QLThumbnailImageCreate(kCFAllocatorDefault, (__bridge CFURLRef)(pa
null
:
NSURL *path = [NSURL URLWithString:@"/Applications/Calculator.app"];
NSLog(@"%@", path);
NSImage *image = (__bridge NSImage *)(QLThumbnailImageCreate(kCFAllocatorDefault, (__bridge CFURLRef)(path), CGSizeMake(100, 100), (__bridge CFDictionaryRef)(@{(NSString *)kQLThumbnailOptionIconModeKey: @NO})));
NSLog(@"%@", image);
您可以使用NSWorkspace,例如
NSImage *image = [[NSWorkspace sharedWorkspace] iconForFile:path];
如果您想使用appbundleId
而不是知道它的路径,您可以先这样做
path = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:bundleIdentifier];
我想您正在寻找[[NSWorkspace sharedWorkspace]iConformFile:pathToFile]
应用程序的图标文件通常在其Info.plist中的cfbundleconfile键下定义。通过获取图标文件名,您可以从bundle的Resources目录(应用程序/Contents/Resources/的路径)获取此图标 要获取Info.plist内容,您可以直接将其加载到NSDictionary中(使用dictionaryWithContentsOfFile:)或从应用程序的bundle对象中获取(例如:
[[NSBundle bundleWithPath:path To anApp]infoDictionary])
图标文件将是CbundleiConfile键的值(即:iconFile=[infoDictionary valueForKey:@“CbundleiConfile”]
。其路径将是:应用程序路径/Contents/Resources/iconFile)