Skip to content

Instantly share code, notes, and snippets.

@markd2
Created February 10, 2017 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markd2/59269054aa38715fc42591932df9e4c5 to your computer and use it in GitHub Desktop.
Save markd2/59269054aa38715fc42591932df9e4c5 to your computer and use it in GitHub Desktop.
- (CGImageRef) newMaskFromImage: (UIImage *) image {
// Prior to 10.2 SDK / Xcode 8 (dunno which it is), the mask creation worked
// for all of the bucket overflow images. It failed afterwards. Dunno if the
// pngcrushing changed the color space unexpectedly, or what. But trying one and
// then falling back to the other seems to work ok now.
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB ();
CGImageRef maskRef = CGImageCreateCopyWithColorSpace (image.CGImage, cs);
if (maskRef == NULL) {
CGColorSpaceRef grayCs = CGColorSpaceCreateDeviceGray ();
maskRef = CGImageCreateCopyWithColorSpace (image.CGImage, grayCs);
if (maskRef == NULL) {
NSLog(@"Could not create overflow mask with either rgb or gray color space");
}
}
CGImageRef mask2 = CGImageMaskCreate (CGImageGetWidth (maskRef),
CGImageGetHeight (maskRef),
CGImageGetBitsPerComponent (maskRef),
CGImageGetBitsPerPixel (maskRef),
CGImageGetBytesPerRow (maskRef),
CGImageGetDataProvider (maskRef),
NULL, NO);
CGColorSpaceRelease (cs);
CGImageRelease (maskRef);
return mask2;
} // newMaskFromImage
- (void) fillMask: (CGImageRef) mask withColor: (UIColor *) color
atOffset: (CGPoint) offset {
if (color == nil) color = [UIColor purpleColor];
CGContextRef context = UIGraphicsGetCurrentContext ();
CGContextSaveGState (context); {
CGContextTranslateCTM (context, offset.x, offset.y);
CGRect rect = { {0.0, 0.0}, { CGImageGetWidth(mask), CGImageGetHeight(mask) } };
CGContextClipToMask (context, rect, mask);
[color set];
UIRectFill (rect);
} CGContextRestoreGState (context);
} // fillMask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment