8

I am doing some drawing using a CGContext. I am currently masking the drawing using a png file like this:

UIImage * myImage = [UIImage imageNamed:@"frame.png"];
CGContextRef context = UIGraphicsGetCurrentContext ();
CGContextClipToMask(context, self.view.bounds, myImage.CGImage);

This works fine, but now I'd like to use an existing CGPathRef as my mask. Should I look at converting the CGPathRef to a UIImage and mask as above? If so how would I go about doing the conversion? -OR- Is there a better way to approach this?

Frank
  • 299
  • 1
  • 4
  • 11

1 Answers1

12
CGContextAddPath(context, yourPath);
CGContextClip(context);
cxa
  • 4,238
  • 26
  • 40
  • 1
    This works good for rects&ellipse. But not works for me if path is created by addArcWithCenter:radius:... And when I need just one pixel line to keep. – Sergey Kopanev Jun 21 '15 at 20:28
  • What should we do if we don't want it to be clipped? For example if we want to mask a custom path on an image without clipping it to the bounds of path ? @cxa – Reza.Ab Feb 04 '18 at 21:19