Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reset or clear the clipping mask associated with a CGContext?

I'm drawing to a CGContext and using a CGImageRef-based mask:

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextClipToMask(context, rect, _firstMaskImageRef);
CGContextSetFillColorWithColor(context, color);
CGContextFillRect(context, rect);

I have a second mask that I then want to switch to:

CGContextClipToMask(context, rect, _secondMaskImageRef);
CGContextSetFillColorWithColor(context, color); // color has changed FWIW
CGContextFillRect(context, rect); // as has rect

But, this intersects the two masks instead of replacing the first.

How do you (if you can) clear out or reset the clipping mask for a CGContext?

like image 249
WiseOldDuck Avatar asked Sep 06 '13 01:09

WiseOldDuck


2 Answers

You can save the graphics state before you set the clipping mask, and then reset it afterwards, like this:

CGContextSaveGState (context);
...Set your first clipping mask, fill it, etc.
CGContextRestoreGState (context);
...Do other stuff
like image 173
user1118321 Avatar answered Nov 11 '22 12:11

user1118321


To reset the clipping mask, use:

CGContextResetClip(context);
like image 26
IjzerenHein Avatar answered Nov 11 '22 13:11

IjzerenHein