如何在剪裁绘图后将上下文重置为原始矩形?

don*_*ile 4 iphone

我尝试绘制一系列图案图像(在一个视图中不同的重复图案).

所以我在循环中做了这个:

CGContextRef context = UIGraphicsGetCurrentContext();

// clip to the drawing rectangle to draw the pattern for this portion of the view
CGContextClipToRect(context, drawingRect);

// the first call here works fine... but for the next nothing will be drawn
CGContextDrawTiledImage(context, CGRectMake(0, 0, 2, 31), [img CGImage]);
Run Code Online (Sandbox Code Playgroud)

我认为在剪切上下文以在特定矩形中绘制图案后,我从大画布中剪切出一个片段,下一次,我的画布消失了.不能删掉另一个片段.所以我必须以某种方式重置剪辑,以便能够在其他地方再次绘制另一个模式?

编辑:在文档中我发现了这个:

CGContextClip:"...因此,要通过将剪切路径恢复到先前状态来重新放大可绘制区域,必须在剪辑之前保存图形状态,并在完成任何剪切绘图后恢复图形状态. ".

那么,如何在剪辑之前存储图形状态以及如何恢复它?

glo*_*ker 20

您正在寻找的功能是:

CGContextSaveGState(context);
Run Code Online (Sandbox Code Playgroud)

CGContextRestoreGState(context);
Run Code Online (Sandbox Code Playgroud)