Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset the context to the original rectangle after clipping it for drawing?

Tags:

iphone

I try to draw a sequence of pattern images (different repeated patterns in one view).

So what I did is this, in a loop:

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]);

I think that after I've clipped the context to draw the pattern in the specific rectangle, I cut out a snippet from the big canvas and the next time, my canvas is gone. can't cut out another snippet. So I must reset that clipping somehow in order to be able to draw another pattern again somewhere else?

Edit: In the documentation I found this:

CGContextClip: "... Therefore, to re-enlarge the paintable area by restoring the clipping path to a prior state, you must save the graphics state before you clip and restore the graphics state after you’ve completed any clipped drawing. ..."

Well then, how to store the graphics state before clipping and how to restore it?

like image 459
dontWatchMyProfile Avatar asked Nov 30 '22 10:11

dontWatchMyProfile


1 Answers

The functions you are looking for are:

CGContextSaveGState(context);

and

CGContextRestoreGState(context);
like image 122
glorifiedHacker Avatar answered Dec 05 '22 23:12

glorifiedHacker