如何使用 Cocoa 将图像重叠(叠加)在文件图标上?
我必须制作一个原型应用程序,在给定文件夹的文件图标上叠加一个小图像。 假设我有一个文件夹 /MyDocuments/ 并且有三个文件 /MyDocuments/Doc1.rtf /MyDocuments/Doc1.pdf 和 /MyDocuments/Doc1.jpg 我有一个图像 myicon.png,现在我必须将此图像 myicon.png 叠加在 /MyDocuments/ 中存在的所有三个文件的文件图标上
我知道我可以使用 < 中的方法strong>NSWorkspace sharedWorkspace 来获取和设置这些文件的文件图标,但我不知道如何使用图像 myicon.png 并将其叠加在这些文件的现有图标上。
如果有人看过 Dropbox 应用程序 (dropbox.com),那么它与您在 Dropbox 文件夹中看到更改的图标的方式类似,
我认为这将使用 NSImage 完成,但我不知道如何做到这一点。
注意:图像 myicon.png 将仅占据这些文件的原始图标的左上部分,即图像不应与现有图标完全重叠,而仅应占据左上的 1/4 部分。
I have to make a prototype application where I superimpose a small image over the file icons of a given folder.
Let's say I have a folder /MyDocuments/
and there are three files /MyDocuments/Doc1.rtf /MyDocuments/Doc1.pdf and /MyDocuments/Doc1.jpg
and I have an image myicon.png, now I have to superimpose this image myicon.png over the file icons of all the three files present in /MyDocuments/
I understand that I can use the methods in NSWorkspace sharedWorkspace to get and set the file icons for these files, but I have no idea how to use the image myicon.png and superimpose it over the existing icons of these files.
If anyone has seen the Dropbox application (dropbox.com), then it is similar to the way you see changed icons in your dropbox folder
I assume it would be done using NSImage but I have no idea how to do it.
Note: the image myicon.png will only occupy the top left part of the original icon of these files i.e. the image should not completely overlap with the existing icons but only the 1/4th portion on the top left should be occupied.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将焦点锁定在文件图标上,然后绘制 徽章图标,然后 解锁焦点 。您可能想要对文件图标的副本执行此操作,并保留未标记的原始图标。
如果徽章是 Mac OS X 附带的标准徽章之一,请勿将徽章复制到您的应用程序中 - 如果 Apple 对其进行更改,它会显得过时。标准徽章在 IconsCore.h 中命名;您可以使用
NSFileTypeForHFSTypeCode
函数将任何这些类型包装在字符串中,然后将该字符串传递给 NSWorkspace 的iconForFileType:
以获取标准徽章作为图像,从中点你可以做到以上。Lock focus on the file icon, then draw the badge icon, then unlock focus. You may want to do this to a copy of the file icon, and hang on to the unbadged original.
If the badge is one of the standard badges that come with Mac OS X, don't copy the badge into your app—it'll look outdated if Apple ever changes it. The standard badges are named in IconsCore.h; you can wrap any of those types in a string using the
NSFileTypeForHFSTypeCode
function, then pass that string to NSWorkspace'siconForFileType:
to get the standard badge as an image, from which point you can do the above.作为 Peter Hosey 答案的补充,这里有一些稍微修改过的示例代码:
http:// cocoadev.com/forums/comments.php?DiscussionID=221
As a supplement to Peter Hosey's answer, here is some slightly modified example code from:
http://cocoadev.com/forums/comments.php?DiscussionID=221