Skip to content

Instantly share code, notes, and snippets.

@mrjjwright
Created July 26, 2011 04:55
Show Gist options
  • Save mrjjwright/1106004 to your computer and use it in GitHub Desktop.
Save mrjjwright/1106004 to your computer and use it in GitHub Desktop.
Fill opaque parts of NSImage with color
+ (NSImage *)maskImageNamed:(NSString *)name color:(NSColor *)color
{
NSImage *image = [NSImage imageNamed:name];
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
CGContextRef c = MyCreateBitmapContext(image.size.width, image.size.height);
[image drawInRect:rect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
CGContextSetFillColorWithColor(c, [color CGColor]);
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
CGContextFillRect(c, rect);
CGImageRef ciImage = CGBitmapContextCreateImage(c);
CGContextDrawImage(c, rect, ciImage);
NSImage *newImage= [[[NSImage alloc] initWithCGImage:ciImage size:image.size] autorelease];
CGContextRelease(c);
CGImageRelease(ciImage);
return newImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment