iOS Quartz 2D相关笔记总结笔记 韩俊强的博客

来源:互联网 发布:英雄联盟网吧特权软件 编辑:程序博客网 时间:2024/06/05 15:37

一、基本理论和基本代码

  1. 概念总结

Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统。Quartz 2D能完成的工作:
绘制图形 : 线条,三角形,矩形,圆,弧等;
绘制文字
绘制,生成图片(图像)
读取,生成PDF
截图,裁剪图片
自定义UI控件

Quartz2D提供了以下几种类型的Graphics Context:
Bitmap Graphics Context
PDF Graphics Context
Window Graphics Context
Layer Graphics Context
Printer Graphics Context

2.基本代码

1).获得图形上下文

CGContextRefctx= UIGraphicsGetCurrentContext();
2).拼接路径
CGContextMoveToPoint(ctx, 10, 10); CGContextAddLineToPoint(ctx, 100, 100);
3).绘制路径
CGContextStrokePath(ctx); CGContextFillPath(cox);

新建一个起点

voidCGContextMoveToPoint(CGContextRefc, CGFloatx, CGFloaty)

添加新的线段到某个点

voidCGContextAddLineToPoint(CGContextRefc, CGFloatx, CGFloaty)

添加一个矩形

CGContextAddRect(CGContextRefc, CGRectrect)

添加一个椭圆

voidCGContextAddEllipseInRect(CGContextRefcontext, CGRectrect)

添加一个圆弧

CGContextAddArc(CGContextRefc, CGFloatx, CGFloaty,CGFloatradius, CGFloatstartAngle, CGFloatendAngle, int clockwise)

Mode参数决定绘制的模式

CGContextDrawPath(CGContextRefc, CGPathDrawingModemode)

绘制空心路径

CGContextStrokePath(CGContextRefc)

绘制实心路径

CGContextFillPath(CGContextRefc)

提示:一般以CGContextDraw、CGContextStroke、CGContextFill开头的函数,都是用来绘制路径的

绘制一个进度条

- (void)drawRect:(CGRect)rect{    CGContextRef ctx = UIGraphicsGetCurrentContext();    CGPoint centerP = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);    CGFloat radius = MIN(rect.size.width, rect.size.height) * 0.5 - 15;    UIBezierPath* path2 = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius startAngle:-M_PI_2 endAngle: M_PI_2 + M_PI*2 clockwise:YES];    [[UIColor redColor] set];    CGContextAddPath(ctx, path2.CGPath);    CGContextDrawPath(ctx, kCGPathStroke);    UIBezierPath* path3 = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius-20 startAngle:-M_PI_2 endAngle: M_PI_2 + M_PI*2 clockwise:YES];    [[UIColor redColor] set];    CGContextAddPath(ctx, path3.CGPath);    CGContextDrawPath(ctx, kCGPathStroke);     UIBezierPath* path = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius-10 startAngle:-M_PI_2 endAngle:M_PI * 2 * self.progress - M_PI_2 clockwise:YES];    CGContextSetLineWidth(ctx, 20);    [[UIColor blueColor] set];    CGContextSetLineCap(ctx, kCGLineCapRound);    CGContextAddPath(ctx, path.CGPath);    CGContextDrawPath(ctx, kCGPathStroke);}


二、常用举例

  1. 颜色转图像

- (UIImage*) createImageWithColor: (UIColor*) color{    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);    UIGraphicsBeginImageContext(rect.size);    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetFillColorWithColor(context, [color CGColor]);    CGContextFillRect(context, rect);    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return theImage;}


2.view截屏

-(UIImage*)captureView:(UIView*)view{    if (!view) {        return nil;    }    UIGraphicsBeginImageContext(view.bounds.size);    [view.layer renderInContext:UIGraphicsGetCurrentContext()];    UIImage* image= UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();        //写入照片        //    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);    return image;}

每日更新关注:http://weibo.com/hanjunqiang  新浪微博!iOS开发者交流QQ群: 446310206



11 0