Quartz 2D

来源:互联网 发布:松下网络摄像机官网 编辑:程序博客网 时间:2024/05/22 03:05

http://blog.sina.com.cn/s/blog_7570f7920101b76h.html

http://donbe.blog.163.com/blog/static/138048021201052093633776/

1、Quartz 2D是二维绘图引擎,是core graphics框架的一部分,在图形的上下文中绘制,图形的上下文可以看做是画布。


2、图形的上下文根据功能(与设备无关)分为5大类:

(1)Bitmap graphics context:位图的上下文,可以绘制RGB颜色

(2)pdf graphics context:图形的上下文转换成pdf格式

(3)window graphics context:将图形的上下文绘制到窗口

(4)layer graphics context:将图形的上下文绘制到图层中

(5)postscript graphics context:针对打印机


3、Quartz 2D的数据类型

由这些特定的数据类型创建出对象,再操作对象的方法得到想要的图形上下文。

CGPathRef、CGImageRef 、CGLayerRef、 CGShadingRef、CGGradientRef、CGFunctionRef 、CGColorRef 、CGColorSpaceRef、CGPatternRef、CGImageSourceRef、CGImageDestinationRef等等。


4、使用Quartz 2D的方法是在UIView的drawRect:中添加绘制图形的代码


5、绘制的路径函数

(1)绘制线段:CGContextMoveToPoint()   设置起点

CGContextAddLineToPoint()  设置终点

CGContextAddLines() 设置多条线的点

(2)绘制矩形:CGContextAddRect()设置一个矩形的大小和位置

CGContextAddRects()

(3)绘制圆:CGContextAddEllipseInRect()设置圆的大小和位置

(4)绘制圆弧:CGContextAddArcToPoint()设置圆弧的终点

CGContextAddArc()设置圆弧

(5)描边或者填充路径:CGContextStrokePath()

CGContextFillPath()

CGContextEOFillPath()

CGContextDrawPath()


6、为图形添加特效

(1)设置线宽

CGContextSetLineWidth()

(2)设置线条颜色

CGContextSetRGBStrokeColor() 默认线条的颜色为黑色

(3)绘制渐变颜色

A、创建渐变色 CGGradientRef CGGradienCreateWithColorComponents(

CGColorSpaceRef space,//指定色彩空间

const CGFloat *components,//指定颜色分量的数组

const CGFloat *locations,//指定颜色数组各颜色的位置,控制从一个颜色过渡到另一个颜色的速度快慢

size_t count//指定位置数组的颜色数量

)

B、绘制线性渐变 void CGContextDrawLinearGradient(

CGContextRef context,

CGGradientRef gradient,

CGPoint startpoint,

CGPoint endpoint,

CGGradientDrawingOptions options

)

C、例子

- (void)drawRect:(CGRect)rect

{

    // Drawing code

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();

    //设置开始的颜色

    UIColor *start = [UIColorredColor];

    CGFloat *startColorCom = (CGFloat *)CGColorGetComponents([startCGColor]);//分解得到RGBA

    //设置结束的颜色

    UIColor *end = [UIColorblueColor];

    CGFloat *endColorCom = (CGFloat *)CGColorGetComponents(end.CGColor);//分解得到RGBA

    //创建颜色分量的数组

   CGFloat colorComponent[8] = {startColorCom[0],startColorCom[1],startColorCom[2],startColorCom[3],endColorCom[0],endColorCom[1],endColorCom[2],endColorCom[3]};

    //指定渐变开始的位置和结束位置

   CGFloat colorIndices[2] = {0.0f,1.0f};

   CGGradientRef gradient =CGGradientCreateWithColorComponents(colorSpace, colorComponent, colorIndices, 2);

   CGPoint startPoint =CGPointMake(120, 260);//渐变起点

   CGPoint endPoint =CGPointMake(200, 220);//渐变终点

   CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);

}


其中常见的特性有,透明的图层layer、基于路径的绘制、高级颜色管理、pdf文件的创建和显示转换等。Quartz 2D。


0 0