iconForFile] 返回具有 0 个表示的图像
我的 Cocoa 应用程序显示给定文件夹中所有文件的文件图标。我使用此代码来获取图标图像:
NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:thePath];
偶尔,返回的图标没有 (0) 图像表示,但大小设置为 32x32(默认大小如文档所述),其他时候是正确的图标(如显示在 Finder 中)已返回。
我曾经检查过 nil
,但我的应用程序稍后会崩溃,直到我发现图像是“空”,可以这么说。
当发生这种情况时,它始终是同一个文件:Safari .webarchive
(该文件夹有两个 webarchives,图标显示第一个......)。
上面的代码有什么特别之处吗?这可能是我的问题吗?无法想象为什么这会成为一个问题,因为该线程没有更新任何 UI 元素,
谢谢!
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您必须在失败时检查某些条件,即大小== NSZeroSize 或图像代表数为零等...然后在返回的 NSImage 无效时准备好占位符图像。
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.
NSWorkspace iconForFile:
状态的文档:所以后台线程应该不是问题。
The docs for
NSWorkspace iconForFile:
state:So the background thread should not be a problem.