Fill a form with a gradient in a CGContext

I want to fill out the polygon shape that I drew through Core Graphics with linear CGGradient. The CGContextDrawLinearGradient function draws a gradient from one point to another, but fills the entire view. How to display the gradient only inside the polygon shape that I drew?

+7
objective-c cocoa cgcontext
source share
1 answer

You can build the CGMutablePath in the desired form, and then use it to clip to the area you want to display, something like ...

// Construct yourClipPath CGContextAddPath(yourContext, yourClipPath); CGContextClosePath(yourContext); CGContextClip(yourContext); // Draw Your Gradient 
+16
source share

All Articles