Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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