If I have a file, I can get the icon by doing something such as:
NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFile: @"myFile.png"];
But if I just wanted to get the icon for a specific file type (example the icon associated with png files, without having a "myFile.png" that already exists), i'm not sure how I can do that.
Any suggestions are appreciated!
Underneath -[NSWorkspace iconForFile:]
in the documentation is -[NSWorkspace iconForFileType:]
. Have you tried that?
You can first determine file type (UTI) and then pass it on to get icon:
NSString *fileName = @"lemur.jpg"; // generic path to some file
CFStringRef fileExtension = (__bridge CFStringRef)[fileName pathExtension];
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
NSImage *image = [[NSWorkspace sharedWorkspace]iconForFileType:(__bridge NSString *)fileUTI];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With