Skip to content

Instantly share code, notes, and snippets.

@toddlee
Created October 10, 2012 08:02
Show Gist options
  • Save toddlee/3863931 to your computer and use it in GitHub Desktop.
Save toddlee/3863931 to your computer and use it in GitHub Desktop.
Masking a UIImage with a mask UIImage in iOS
- (UIImage *)maskImage:(UIImage *)image withMask:(UIImage *)maskImage
{
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
return [UIImage imageWithCGImage:masked];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment