//1.获得图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.拼接路径(下面代码是搞一条线段)
CGContextMoveToPoint(ctx, 10, 10);
CGContextAddLineToPoint(ctx, 100, 100);
//3.绘制路径
CGContextStrokePath(ctx); // CGContextFillPath(ctx);
//新建一个起点
void CGContextMoveToPoint(CGContextRef c, CGFloat x, CGFloat y)
//添加新的线段到某个点
void CGContextAddLineToPoint(CGContextRef c, CGFloat x, CGFloat y)
//添加一个矩形
void CGContextAddRect(CGContextRef c, CGRect rect)
//添加一个椭圆
void CGContextAddEllipseInRect(CGContextRef context, CGRect rect)
//添加一个圆弧
void CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y,
CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)
//Mode参数决定绘制的模式
void CGContextDrawPath(CGContextRef c, CGPathDrawingMode mode)
//绘制空心路径
void CGContextStrokePath(CGContextRef c)
//绘制实心路径
void CGContextFillPath(CGContextRef c)
//提示:一般以CGContextDraw、CGContextStroke、CGContextFill开头的函数,都是用来绘制路径的
//将当前的上下文copy一份,保存到栈顶(那个栈叫做”图形上下文栈”)
void CGContextSaveGState(CGContextRef c)
//将栈顶的上下文出栈,替换掉当前的上下文
void CGContextRestoreGState(CGContextRef c)
//缩放
void CGContextScaleCTM(CGContextRef c, CGFloat sx, CGFloat sy)
//旋转
void CGContextRotateCTM(CGContextRef c, CGFloat angle)
//平移
void CGContextTranslateCTM(CGContextRef c, CGFloat tx, CGFloat ty)
//开启一个基于位图的图形上下文
void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)
//从上下文中取得图片(UIImage)
UIImage* UIGraphicsGetImageFromCurrentImageContext();
//结束基于位图的图形上下文
void UIGraphicsEndImageContext();
void CGContextClip(CGContextRef c)
//将当前上下所绘制的路径裁剪出来(超出这个裁剪区域的都不能显示)
- (void)renderInContext:(CGContextRef)ctx;
调用某个view的layer的renderInContext:方法即可