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

I draw in a CGContext and using a mask based on CGImageRef:

CGContextRef context = UIGraphicsGetCurrentContext(); CGContextClipToMask(context, rect, _firstMaskImageRef); CGContextSetFillColorWithColor(context, color); CGContextFillRect(context, rect); 

I have a second mask, which 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 crosses two masks instead of replacing the first.

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

+6
source share
3 answers

You can save the graphical state before setting the mask, and then reset after that, for example:

 CGContextSaveGState (context); ...Set your first clipping mask, fill it, etc. CGContextRestoreGState (context); ...Do other stuff 
+15
source

In later versions of Swift (3+?), Use the following syntax instead:

 context.saveGState() // set your clipping mask, etc. context.restoreGState() // everything back to normal 
0
source

In reset clipping mask use:

 CGContextResetClip(context); 
0
source

Source: https://habr.com/ru/post/953287/


All Articles