Skip to content

Instantly share code, notes, and snippets.

@guidedways
Forked from toddlee/masking-image.m
Created December 29, 2019 12:08
Show Gist options
  • Save guidedways/9262ddf947d846d6830d25ed525f959f to your computer and use it in GitHub Desktop.
Save guidedways/9262ddf947d846d6830d25ed525f959f to your computer and use it in GitHub Desktop.
Masking a UIImage with a mask UIImage in iOS
- (UIImage*) maskImageWithMask:(UIImage *) mask
{
CGImageRef imageReference = self.CGImage;
CGImageRef maskReference = mask.CGImage;
CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(maskReference),
CGImageGetHeight(maskReference),
CGImageGetBitsPerComponent(maskReference),
CGImageGetBitsPerPixel(maskReference),
CGImageGetBytesPerRow(maskReference),
CGImageGetDataProvider(maskReference),
NULL, // Decode is null
YES // Should interpolate
);
CGImageRef maskedReference = CGImageCreateWithMask(imageReference, imageMask);
CGImageRelease(imageMask);
UIImage *maskedImage = [UIImage imageWithCGImage:maskedReference];
CGImageRelease(maskedReference);
return maskedImage;
}
@guidedways
Copy link
Author

Original was leaking memory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment