9

如何使用 Objective-C 从另一个应用程序获取图标?

到目前为止我已经尝试过了,但是它只是返回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);
4

3 回答 3

22

您可以使用 NSWorkspace,例如

NSImage *image = [[NSWorkspace sharedWorkspace] iconForFile:path];

如果你想使用应用程序bundleId而不是知道它的路径,你可以先这样做

path = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:bundleIdentifier];
于 2012-08-28T19:54:14.343 回答
3

我认为您正在寻找 [[NSWorkspace sharedWorkspace] iconForFile:pathToFile]

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html#//apple_ref/doc/uid/20000391-iconForFile_

于 2012-08-28T19:56:39.530 回答
3

应用程序的图标文件通常在 CFBundleIconFile 键下的 Info.plist 中定义。通过获取图标文件名,您可以从包的资源目录(路径到应用程序/内容/资源/)中获取此图标。

要获取 Info.plist 内容,您可以直接在 NSDictionary 中加载它(使用 dictionaryWithContentsOfFile:) 或从应用程序的 bundle 对象中获取它(例如 : [[NSBundle bundleWithPath:path-to-anApp] infoDictionary]).

图标文件将是 CFBundleIconFile 键的值(即:iconFile = [infoDictionary valueForKey:@"CFBundleIconFile"]。其路径将是:path-to-anApplication/Contents/Resources/iconFile)。

于 2012-08-28T20:09:46.340 回答