Created
August 17, 2011 14:53
-
-
Save zachwaugh/1151687 to your computer and use it in GitHub Desktop.
Image mask
This file contains hidden or 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
- (UIImage *)imageWithMask:(UIImage *)maskImage andIsWhite:(BOOL)isWhite | |
{ | |
CGRect imageRect = CGRectMake(0, 0, maskImage.size.width, maskImage.size.height); | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
CGContextRef ctx = CGBitmapContextCreate(NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); | |
CGContextClipToMask(ctx, imageRect, maskImage.CGImage); | |
if (isWhite) { | |
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1); | |
} else { | |
CGContextSetRGBFillColor(ctx, 0, 0, 0, 1); | |
} | |
CGContextFillRect(ctx, imageRect); | |
CGImageRef imageRef = CGBitmapContextCreateImage(ctx); | |
UIImage *image = [UIImage imageWithCGImage:imageRef]; | |
CGImageRelease(imageRef); | |
CGContextRelease(ctx); | |
CGColorSpaceRelease(colorSpace); | |
return image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment