如何重置或清除与CGContext关联的剪贴蒙版?

问题描述 投票:7回答:3

我正在绘制CGContext并使用基于CGImageRef的掩码:

CGContextRef context = UIGraphicsGetCurrentContext();

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

我有第二个掩码,然后我想切换到:

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

但是,这会交叉两个蒙版而不是替换第一个蒙版。

你如何(如果可以的话)清除或重置CGContext的剪贴蒙版?

iphone core-graphics cgcontext
3个回答
17
投票

您可以在设置剪贴蒙版之前保存图形状态,然后重置它,如下所示:

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

4
投票

要重置剪贴蒙版,请使用:

CGContextResetClip(context);

0
投票

更高版本的Swift(3+?)使用以下语法:

context.saveGState()
// set your clipping mask, etc.
context.restoreGState()
// everything's back to normal
© www.soinside.com 2019 - 2024. All rights reserved.