二 、Quartz 2D 图形上下文栈

图形上下文,保存着绘图信息。图形上下文栈,保存着图形上下文。

当,CGContextSaveGState(context); 时,他会在图形上下文栈中保存一个图形上下文。

当,CGContextRestoreGState(context); 时,他会删除当前的图像上下文。然后取出图形上下文栈中保存的图形上下文,用作当前图形上下文。

每次都是从栈顶取,CGContextRestoreGState(context);的次数,不能大于CGContextSaveGState(context);。可以多次保存,即CGContextSaveGState(context);

 

看图

 

 

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 保存一份图形上下文栈
    CGContextSaveGState(context);
    
    CGContextSetLineWidth(context, 3);
    CGContextAddEllipseInRect(context, CGRectMake(20, 20, 100, 100));
    CGContextStrokePath(context);
    
    // 取出保存的图形上下文栈
    CGContextRestoreGState(context);
    
    CGContextAddEllipseInRect(context, CGRectMake(120, 220, 100, 100));
    CGContextStrokePath(context);
    
}

 

posted @ 2016-04-10 19:43  人生路1/5  阅读(237)  评论(0)    收藏  举报