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







1 Comment