views:

889

answers:

1

I want to fill a polygon shape that I have drawn via Core Graphics with a linear CGGradient. The CGContextDrawLinearGradient function draws a gradient from one point to another but it fills the entire view. How can I display the gradient only inside the polygon shape I have drawn?

+5  A: 

You can construct a CGMutablePath in your desired shape then use it to clip to the region you want to display, something like...

// Construct yourClipPath

CGContextAddPath(yourContext, yourClipPath);
CGContextClosePath(yourContext);
CGContextClip(yourContext);

// Draw Your Gradient
tmh
Thanks, that works! I figured out that I had to call CGContextSaveGState/CGContextRestoreGState to unclip the region.
titaniumdecoy