Search code examples
cocoansimage

iconForFile] returns image with 0 representations


My Cocoa application displays file icons for all files in a given folder. I'm using this code to get the icon images:

NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:thePath];

Once in a while, the returned icon has no (0) image representations, but a size set to 32x32 (default size as the documentation says), other times the correct icon (as shown in Finder) is returned.

I used to check for nil, but my app would crash later until I discovered that the image was "empty', so to speak.

When it happens, it is always the same file: a Safari .webarchive (the folder has two webarchives, the icon shows for the first one...).

Is there something special about the code above? This is running in a background thread. Could this be the problem? I can't imagine why it should be a problem, as no UI elements are updated from that thread.

Thanks! Mark.


Solution

  • It sounds like you would have to check for some conditions when it is failing i.e. the size == NSZeroSize or the number of image reps is zero, etc... and then have a placeholder image ready for when the returned NSImage is invalid.