CGImageCreateWithMask with an image as a mask

Posted by Peyman on Stack Overflow See other posts from Stack Overflow or by Peyman
Published on 2010-05-31T02:39:41Z Indexed on 2010/05/31 2:42 UTC
Read the original article Hit count: 657

Filed under:

Hi

I am trying to use an image (painted as Core Graphics) to create a mask. What I am doing is this

1. creating a Core Graphics path

    CGContextSaveGState(context);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context,circleCenter.x,circleCenter.y);
    //CGContextSetAllowsAntialiasing(myBitmapContext, YES);
    CGContextAddArc(context,circleCenter.x, circleCenter.y,circleRadius,startingAngle, endingAngle, 0); // 0 is counterclockwise
    CGContextClosePath(context);
    CGContextSetRGBStrokeColor(context,1.0,0.0,0.0,1.0);
    CGContextSetRGBFillColor(context,1.0,0.0,0.0,0.2);
    CGContextDrawPath(context, kCGPathFillStroke);

2. then I'm saving the context that has the path just painted

    CGImageRef pacmanImage              = CGBitmapContextCreateImage (context); 

3. restoring the context

CGContextRestoreGState(context);
CGContextSaveGState(context);

4. creating a 1 bit mask (which will provide the black-white mask)

bitsPerComponent                    = 1;
bitsPerPixel                        = bitsPerComponent * 1 ;
bytesPerRow                         = (CGImageGetWidth(imgToMaskRef) * bitsPerPixel);
mask                                = CGImageCreate(CGImageGetWidth(imgToMaskRef), 
                                                CGImageGetHeight(imgToMaskRef), 
                                                bitsPerComponent,
                                                bitsPerPixel, 
                                                bytesPerRow, 
                                                greyColorSpace,
                                                kCGImageAlphaNone, 
                                                CGImageGetDataProvider(pacmanImage), 
                                                NULL, //decode
                                                YES, //shouldInterpolate
                                                kCGRenderingIntentDefault);

5. masking the imgToMaskRef (which is a CGImageRef imgToMaskRef =imgToMask.CGImage;) with the mask just created

    imageMaskedWithImage                = CGImageCreateWithMask(imgToMaskRef, mask);
CGContextDrawImage(context,imgRectBox, imageMaskedWithImage);
CGImageRef maskedImageFinal         = CGBitmapContextCreateImage (context);
  1. returning the maskedImageFinal to the caller of this method (as wheelChoiceMadeState, which is a CGImageRef) who then updates the CALayer contents property with the image

    theLayer.contents = (id) wheelChoiceMadeState;

the problem I am seeing is that the mask does not work properly and looks very strange indeed. I get strange patterns across the path painted by the Core Graphics. My hunch is there is something with CGImageGetDataProvider() but I am not sure.

Any help would be appreciated

thank you

© Stack Overflow or respective owner

Related posts about cgimagecreatewithmask