如何使用 Cocoa 将图像重叠(叠加)在文件图标上?

发布于 2024-08-22 14:52:13 字数 564 浏览 3 评论 0原文

我必须制作一个原型应用程序,在给定文件夹的文件图标上叠加一个小图像。 假设我有一个文件夹 /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

家住魔仙堡 2024-08-29 14:52:13

将焦点锁定在文件图标上,然后绘制 徽章图标,然后 解锁焦点 。您可能想要对文件图标的副本执行此操作,并保留未标记的原始图标。

如果徽章是 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's iconForFileType: to get the standard badge as an image, from which point you can do the above.

上课铃就是安魂曲 2024-08-29 14:52:13

作为 Peter Hosey 答案的补充,这里有一些稍微修改过的示例代码:

http:// cocoadev.com/forums/comments.php?DiscussionID=221

NSImage *origImage = [sourceImage copy]; // Copy to avoid modifying the original.

NSSize previewSize = NSMakeSize([origImage size].width / 4.0, [origImage size].height / 4.0);
NSImage *previewImage = [[NSImage alloc] initWithSize:previewSize];
[previewImage lockFocus];
[origImage drawInRect:NSMakeRect(0, 0, previewSize.width, previewSize.height)
             fromRect:NSZeroRect // Draws full image.
            operation:NSCompositeSourceOver
             fraction:1.0];
[previewImage unlockFocus];

As a supplement to Peter Hosey's answer, here is some slightly modified example code from:

http://cocoadev.com/forums/comments.php?DiscussionID=221

NSImage *origImage = [sourceImage copy]; // Copy to avoid modifying the original.

NSSize previewSize = NSMakeSize([origImage size].width / 4.0, [origImage size].height / 4.0);
NSImage *previewImage = [[NSImage alloc] initWithSize:previewSize];
[previewImage lockFocus];
[origImage drawInRect:NSMakeRect(0, 0, previewSize.width, previewSize.height)
             fromRect:NSZeroRect // Draws full image.
            operation:NSCompositeSourceOver
             fraction:1.0];
[previewImage unlockFocus];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文