Ios 在Quartz 2D中绘制时回收以前的画布

Ios 在Quartz 2D中绘制时回收以前的画布,ios,objective-c,canvas,quartz-2d,Ios,Objective C,Canvas,Quartz 2d,我希望在重画视图时能够在现有画布上绘制。这怎么可能 这就是我目前的绘画方式: - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); // save the current state, as we'll overwrite this CGContextSaveGState(context); /* draw a line across

我希望在重画视图时能够在现有画布上绘制。这怎么可能

这就是我目前的绘画方式:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    // save the current state, as we'll overwrite this
    CGContextSaveGState(context);

    /*
     draw a line across the top of the view
     */
    // move the pen to the starting point
    CGContextMoveToPoint(context, 50, 10);
    // draw a line to another point
    CGContextAddLineToPoint(context, 190, 60);

    /*
     draw a rectangle just below, with a stroke on the outside.
     */
    CGContextAddRect(context, CGRectMake(60, 100, 180, 90));

    /*
     write the previous to the context then
     change the colour to blue and the stroke to 2px.
     */
    CGContextStrokePath(context);
    CGContextSetRGBStrokeColor(context, 1, 1, 1, 1);
    CGContextSetLineWidth(context, 2);

    /*
     draw a circle filling most of the rest of the box.
     */
    CGContextAddEllipseInRect(context, CGRectMake(100, 100, 100, 100));

    // do the actual drawing
    CGContextStrokePath(context);
    // restore the state back after drawing on it.
    CGContextRestoreGState(context);
    }

需要的解决方案伪代码/行为:

- (void)drawRect:(CGRect)rect
{
      Retrieve previously drawn canvas 
       If available then 
            Create new drawings 
            Draw them on top of previous canvas
       If not then
            Create drawings
            Draw them on canvas
            Save canvas so that is available for the next drawRect call
}

在结束时调用CGContextRestoreGState是否有效?否,因为路径信息未保存在“上下文”中。我正在尝试保留对CGMutablePathRef的引用,但在这方面没有太大成功。。