27
You might be able to use CGContextClipToMask:
-(void) drawRect:(CGRect)dirty {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState( context );
CGContextClipToMask( context , [self bounds] , [self maskImage] );
[super drawRect:dirty]; // or draw your color manually
CGContextRestoreGState( context );
}
Thanks drawnonward,
ended up with this function:
- (UIImage*) maskImage:(UIView *)image withMask:(UIImage *)maskImage {
UIGraphicsBeginImageContext(image.bounds.size);
[image.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask([viewImage CGImage], mask);
return [UIImage imageWithCGImage:masked];
}