开发者

iconForFile] returns image with 0 representations

开发者 https://www.devze.com 2023-02-15 17:44 出处:网络
My Cocoa application displays file icons for all files in a given folder. I\'m using this code to get the icon images:

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.


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.


This is running in a background thread. Could this be the problem?

The docs for NSWorkspace iconForFile: state:

Discussion

The returned image has an initial size of 32 pixels by 32 pixels.

It is safe to call this method from any thread of your app.

So the background thread should not be a problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号