Brian Dunagan

January 25 2010
Cocoa Tip: NSImage Composites

Recently, I wanted to composite together two images, much like Tweetie’s retweet UI for avatars: display the main icon in its original size but overlay a second icon smaller and offset. Turns out to be only a couple lines of code using NSImage:

NSImage *mainIcon = [NSImage imageNamed:@"main_icon"];
NSImage *overlayIcon = [NSImage imageNamed:@"overlay_icon"];
[mainIcon lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[overlayIcon drawInRect:NSMakeRect([mainIcon size].width / 2, 0, [mainIcon size].width / 2, [mainIcon size].width / 2) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[mainIcon unlockFocus];
Creating a Menu Bar Item Obligatory iPad Post
LinkedIn GitHub Email