iPhone Quartz. How do I create a matte using a path rather then a bitmap?

548 Views Asked by At

In the visualization app I'm writing I want to shape a graphic using a matte created with a path. The graphic is a horizontal rectangular strip with various shapes drawn within. Atop this rectangular strip I want to draw an ellipse to act as a matte that shapes the rectangular strip to make it appear to drawn within the ellipse. How do I do this?

In this simplified example, I am trying - and failing to make the background blue rectangle to appear as a blue ellipes by drawing an ellipse atop it using various blend modes.

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    [[colorDictionary objectForKey:@"blueSolid"] setFill];
    CGContextFillRect(context, self.bounds);

    //CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGContextSetBlendMode(context, kCGBlendModeDestinationIn);

    CGContextFillEllipseInRect(context, self.bounds);


}

Thanks, Doug

UPDATE: SOLUTION In brief: clipping paths. Dooh! The following code snippet creates the effect I am after which is a blue ellipse against a red background. Note, the blue ellipse is created by constraining a blue rectangle to the shape of the ellipse. This is what I need. Cool.

Hope this helps someone else. Clipping paths are super powerful. Cheers.

CGContextRef context = UIGraphicsGetCurrentContext();

[[colorDictionary objectForKey:@"redSolid"] setFill];
CGContextFillRect(context, self.bounds);

CGContextBeginPath(context);
CGContextAddEllipseInRect(context, CGRectInset(self.bounds, 32, 32));
CGContextClip(context);

[[colorDictionary objectForKey:@"blueSolid"] setFill];
CGContextFillRect(context, self.bounds);
1

There are 1 best solutions below

0
On

In brief: clipping paths. Dooh! The following code snippet creates the effect I am after which is a blue ellipse against a red background. Note, the blue ellipse is created by constraining a blue rectangle to the shape of the ellipse. This is what I need. Cool.

Hope this helps someone else. Clipping paths are super powerful. Cheers.

CGContextRef context = UIGraphicsGetCurrentContext();

[[colorDictionary objectForKey:@"redSolid"] setFill];
CGContextFillRect(context, self.bounds);

CGContextBeginPath(context);
CGContextAddEllipseInRect(context, CGRectInset(self.bounds, 32, 32));
CGContextClip(context);

[[colorDictionary objectForKey:@"blueSolid"] setFill];
CGContextFillRect(context, self.bounds);