Cocoa icon for file type?

后端 未结 4 843
轮回少年
轮回少年 2021-02-05 15:39

If I have a file, I can get the icon by doing something such as:

NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFile: @\"myFile.png\"];
4条回答
  •  时光说笑
    2021-02-05 16:15

    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];
    

提交回复
热议问题