views:

208

answers:

2

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.

+3  A: 

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.

Peter Hosey
This sounds soo simple, thanks Peter
Ashish
A: 

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];
Dan Reese