Skip to content

Instantly share code, notes, and snippets.

@zachwaugh
Created August 17, 2011 14:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachwaugh/1151687 to your computer and use it in GitHub Desktop.
Save zachwaugh/1151687 to your computer and use it in GitHub Desktop.
Image mask
- (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