我正在绘制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的剪贴蒙版?
您可以在设置剪贴蒙版之前保存图形状态,然后重置它,如下所示:
CGContextSaveGState (context);
...Set your first clipping mask, fill it, etc.
CGContextRestoreGState (context);
...Do other stuff
要重置剪贴蒙版,请使用:
CGContextResetClip(context);
更高版本的Swift(3+?)使用以下语法:
context.saveGState()
// set your clipping mask, etc.
context.restoreGState()
// everything's back to normal