user2381688
user2381688

Reputation: 141

NSWorkspace iconForFile: returns image of low quality

I'm using iconForFile: selector of NSWorkspace class, but it returns an image of low quality.

Can anyone tell me why is this happening? Is there any switch or flag to tell the framework to return an image in a different format?

Upvotes: 6

Views: 1188

Answers (2)

Morten J
Morten J

Reputation: 914

A Swift extension that lets you define your desired height (width) using Justin's solution

extension NSWorkspace {
    func icon(for url: URL, height: Int) -> NSImage {
        let image = icon(forFile: url.path)
        image.size = NSSize(width: height, height: height)
        return image
    }
}

Upvotes: 2

justin
justin

Reputation: 104698

The docs state the returned icon's size is 32x32, but you can use -[NSImage setSize:] and it may load a larger representation:

NSImage * iconImage = [[NSWorkspace sharedWorkspace] iconForFile:file];
[iconImage setSize:NSMakeSize(128, 128)];

You can also use Icon Services (e.g. GetIconRefFromFileInfo) or QuickLook (e.g. QLThumbnailImageCreate) for a file's icon or preview icon at a larger size.

Upvotes: 6

Related Questions