贝塞尔曲线相关

UI进阶13 Quartz2D

2016-08-28  本文已影响117人  SoManyDumb

Quartz2D

什么是Quartz2D

Quartz2D实例

Quartz2D在iOS开发中的价值

图形上下文

自定义view

drawRect:

Quartz2D须知

drawRect:中取得的上下文

Quartz2D绘图的代码步骤

//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)

Quartz2D的内存管理

图片水印

//开启一个基于位图的图形上下文
void     UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)
//从上下文中取得图片(UIImage)
UIImage* UIGraphicsGetImageFromCurrentImageContext();
//结束基于位图的图形上下文
void     UIGraphicsEndImageContext();

图片裁剪

void CGContextClip(CGContextRef c)
//将当前上下所绘制的路径裁剪出来(超出这个裁剪区域的都不能显示)

屏幕截图

   - (void)renderInContext:(CGContextRef)ctx;
调用某个view的layer的renderInContext:方法即可
上一篇 下一篇

猜你喜欢

热点阅读