One Quickie


Pouring color through a mask (Graphics->General)
Given a CG Image mask, fill it with color.
- (void) fillMask: (CGImageRef) mask  withColor: (UIColor *) color
         atOffset: (CGPoint) offset {

    if (color == nil) color = [UIColor purpleColor];  // everybody loves purple

    CGContextRef context = UIGraphicsGetCurrentContext ();
    CGSize size = self.bounds.size;

    CGContextSaveGState (context); {
        CGContextTranslateCTM (context, offset.x, offset.y);
        // Account for retina-sized graphics.
        CGContextScaleCTM (context, size.width / CGImageGetWidth(mask),
                           size.height / CGImageGetHeight(mask));
        CGRect rect = { {0.0, 0.0}, { CGImageGetWidth(mask), CGImageGetHeight(mask) } };
        CGContextClipToMask (context, rect, mask);
        [color set];
        UIRectFill (rect);
    } CGContextRestoreGState (context);

} // fillMask



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com