Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling a shape with a gradient in CGContext

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?

like image 394
titaniumdecoy Avatar asked Jun 22 '09 08:06

titaniumdecoy


1 Answers

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
like image 122
tmh Avatar answered Oct 19 '22 07:10

tmh