Cocoa icon for file type?

后端 未结 4 865
轮回少年
轮回少年 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:26

    Here is the Swift 5 version of Dave DeLong's answer:

    icon(forFile:)

    Returns an image containing the icon for the specified file.

    Declaration

    func icon(forFile fullPath: String) -> NSImage
    

    Parameters

    fullPath
    The full path to the file.

    icon(forFileType:)

    Returns an image containing the icon for files of the specified type.

    Declaration

    func icon(forFileType fileType: String) -> NSImage
    

    Parameters

    fileType
    The file type, which may be either a filename extension, an encoded HFS file type, or a universal type identifier (UTI).

提交回复
热议问题