Instantly share code, notes, and snippets.

Embed
What would you like to do?
- (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