Created
July 26, 2011 04:55
-
-
Save mrjjwright/1106004 to your computer and use it in GitHub Desktop.
Fill opaque parts of NSImage with color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (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