您的位置:首页 > 其它

OC画笔CGContextRef

2015-11-26 14:02 323 查看
1.画线

CGContextRef context = UIGraphicsGetCurrentContext();//context相当于画布

CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);//设置线得颜色

CGContextMoveToPoint(context, 100, 100);//画笔的起始点

CGContextAddLineToPoint(context, 150, 150);//画笔的终点

CGContextStrokePath(context);//绘画结束

2.画圆

CGContextRef context = UIGraphicsGetCurrentContext();//context相当于画布

//CGContextAddArc(<#CGContextRef c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFloat endAngle#>, <#int clockwise#>)

CGContextAddArc(context, 100, 100, 50, M_PI, M_PI*3/2, 0);// x y 相当于圆心 radius半径 startAngle起始弧度 endAngle终止弧度 clockwise 0为顺时针,1为逆时针

CGContextStrokePath(context);

3.画一个有填充色的矩形

CGContextRef context = UIGraphicsGetCurrentContext();//得到画布

CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);//设置矩形的填充色

CGContextAddRect(context, CGRectMake(100, 200, 100, 200));// x,y起始点 width 宽度 height 高度

CGContextFillPath(context);//填充矩形

CGContextStrokePath(context);//绘画结束

4.画图

UIGraphicsBeginImageContext(self.view.bounds.size); //currentView 当前的view 创建一个基于位图的图形上下文并指定大小
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];//renderInContext呈现接受者及其子范围到指定的上下文
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();//返回一个基于当前图形上下文的图片
UIGraphicsEndImageContext();//移除栈顶的基于当前位图的图形上下文
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);//然后将该图片保存到图片图
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: