Get icon for another application in Objective-C

吃可爱长大的小学妹 提交于 2019-12-20 20:41:30

问题


How do I get the icon from another application using Objective-C?

I have tried this so far, however it just returns 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);

回答1:


You can use NSWorkspace, e.g.

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

If you want to use the app bundleId instead of knowing its path, you can do this first

path = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:bundleIdentifier];



回答2:


I think you're looking for [[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_




回答3:


The icon file of an application is usually defined in its Info.plist under the CFBundleIconFile key. By obtaining the icon file name, you can then get this icon from the Resources directory of a bundle (path-to-anApplication/Contents/Resources/).

To get the Info.plist contents, you can either load it directly in an NSDictionary (using dictionaryWithContentsOfFile:) or get it from the bundle object of the application (example : [[NSBundle bundleWithPath:path-to-anApp] infoDictionary]).

The icon file will be the value of the CFBundleIconFile key (i.e.: iconFile = [infoDictionary valueForKey:@"CFBundleIconFile"]. Its path will then be : path-to-anApplication/Contents/Resources/iconFile).



来源:https://stackoverflow.com/questions/12166532/get-icon-for-another-application-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!