How to overlay (overlay) an image on a file icon using Cocoa?

I need to create a prototype application where I overlay a small image on the file icons of this folder. Say I have a folder / MyDocuments / and there are three files / MyDocuments / Doc 1.rtf / MyDocuments / Doc1.pdf and / MyDocuments / Doc1.jpg and I have an image myicon.png , now I have to overlay this image on myicon .png to file icons of all 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 myicon.png image and overlay it on top of the existing icons of these files.

If someone saw the Dropbox app (dropbox.com), then it looks like the way you see the changed icons in the Dropbox folder

I assume this will be done using NSImage, but I do not know how to do this.

Note. The image myicon.png will occupy only the upper left part of the original icon of these files, i.e. the image should not completely overlap with existing icons, but only 1/4 of the upper left should be occupied.

+3
source share
2 answers

, draw , . , .

, Mac OS X, - , Apple - . IconsCore.h; NSFileTypeForHFSTypeCode, NSWorkspace iconForFileType:, , .

+5

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];
+4

Source: https://habr.com/ru/post/1733198/


All Articles