存取上下文
`
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextMoveToPoint(ctx, 80, 100);
CGContextAddLineToPoint(ctx, 100, 200);
[[UIColor greenColor]set];
CGContextSetLineWidth(ctx, 5);
CGContextStrokePath(ctx);CGContextRestoreGState(ctx); CGContextMoveToPoint(ctx, 50, 80); CGContextAddLineToPoint(ctx, 200, 300); [[UIColor redColor]set]; CGContextSetLineWidth(ctx, 20); CGContextStrokePath(ctx);
`
矩阵操作
`
CGContextRef ctx = UIGraphicsGetCurrentContext();//旋转的时候,是整个layer都旋转 CGContextRotateCTM(ctx, M_PI_4/2); //缩放比例 CGContextScaleCTM(ctx, 0.5, 1.5); //平移,x轴,y轴 CGContextTranslateCTM(ctx, 50, 100); CGContextAddRect(ctx, CGRectMake(150, 100, 100, 100)); CGContextAddEllipseInRect(ctx, CGRectMake(200, 200, 50, 50)); CGContextStrokePath(ctx);
`
图片剪裁
CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 60, 60)); CGContextClip(ctx); UIImage *image = [UIImage imageNamed:@"12"]; [image drawAtPoint:CGPointMake(100, 100)];