温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS开发CGContextRef画图怎么使用

发布时间:2022-04-27 10:28:52 来源:亿速云 阅读:251 作者:iii 栏目:开发技术

这篇“iOS开发CGContextRef画图怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“iOS开发CGContextRef画图怎么使用”文章吧。

1.创建画布

CGContextRef ctx = UIGraphicsGetCurrentContext();

2.设置属性

//旋转,注意:设置操作必须要在添加图形之前,如果设置在添加图形之后的话,此时它已经画完了,无效
//旋转的时候,是整个layer都旋转了
//旋转45度
CGContextRotateCTM(ctx, M_PI_4);
//缩放:x方向缩放0.5倍,y方向缩放1.5倍
CGContextScaleCTM(ctx, 0.5, 1.5);
//平移:x方向移动50,y方向移动100
CGContextTranslateCTM(ctx, 50, 100);
CGContextSetLineJoin(ctx, kCGLineJoinRound);
//线条宽度
CGContextSetLineWidth(ctx, 1.0);
//起点和终点圆角
CGContextSetLineCap(ctx, kCGLineCapRound);
//转角圆角
CGContextSetLineJoin(ctx, kCGLineJoinRound);
//透明度
CGContextSetAlpha(ctx, 0.5)

3.画直线

//起点
CGContextMoveToPoint(ctx, 10.0, 100.0);
//终点
CGContextAddLineToPoint(ctx, self.frame.size.width-20.0, 100.0);
//颜色 两种设置颜色的方式都可以
//CGContextSetRGBStrokeColor(ctx, 0, 1.0, 0, 1.0);
[[UIColor redColor] set];
//渲染,直线只能绘制空心的,不能调用CGContextFillPath(ctx)
// 或者使用这个方法:CGContextDrawPath(ctx, kCGPathStroke);
CGContextStrokePath(ctx);

或者使用下面方法画直线

CGPoint point[2];//坐标点  
point[0] = CGPointMake(10.0, 100.0);//起点  
point[1] = CGPointMake(self.frame.size.width-20.0, 100.0);//终点   
//points[]坐标数组,和count大小  
CGContextAddLines(context, aPoints, 2);//添加线  
CGContextDrawPath(context, kCGPathStroke);

4.画虚线

//设置虚线颜色
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
//设置虚线绘制起点
CGContextMoveToPoint(ctx, 10.0, 50.0);
//设置虚线绘制终点
CGContextAddLineToPoint(ctx, self.frame.size.width-20.0, 50.0);
//设置虚线排列的宽度间隔:下面的arr中的数字表示先绘制3个点再绘制1个点
CGFloat arr[] = {3, 2};
//下面最后一个参数“2”代表排列的个数。
CGContextSetLineDash(ctx, 0, arr, 2);
CGContextDrawPath(ctx, kCGPathStroke);

5.画三角形

//起点
CGContextMoveToPoint(ctx, self.center.x, 200.0);
//拐点1
CGContextAddLineToPoint(ctx, self.center.x-50.0, 250.0);
//终点
CGContextAddLineToPoint(ctx, self.center.x+50.0, 250.0);
//颜色 两种设置颜色的方式都可以
//CGContextSetRGBStrokeColor(ctx, 0, 1.0, 0, 1.0);
[[UIColor redColor] set];
//合并三角形
CGContextClosePath(ctx);
CGContextFillPath(ctx);

6.画矩形

CGRect rectangle = CGRectMake(10.0, 300.0, self.frame.size.width-20.0, 60.0);
CGContextAddRect(ctx, rectangle);
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextFillPath(ctx);

7.画圆

/**
c           当前图形
x           圆心坐标x
y           圆心坐标y
radius      半径
startAngle  弧的起点与正X轴的夹角
endAngle    弧的终点与正X轴的夹角
clockwise   指定0创建一个顺时针的圆弧,或是指定1创建一个逆时针圆弧
*/
CGContextAddArc(ctx, self.center.x, 100.0, 75.0, 0.0, M_PI+0.5, 0);
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextFillPath(ctx);

8.画椭圆

CGContextAddEllipseInRect(ctx, CGRectMake(x, y, 100.0, 60.0));
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextFillPath(ctx);

9.画扇形

CGContextMoveToPoint(ctx, x, y);
CGContextAddArc(ctx, x, y, 75.0, 0.0, M_PI+0.5, 0);
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
CGContextDrawPath(ctx, kCGPathFillStroke);

10.画二次贝塞尔曲线

CGContextMoveToPoint(context, 120, 300);//设置Path的起点  
CGContextAddQuadCurveToPoint(context,190, 310, 120, 390);//设置贝塞尔曲线的控制点坐标和终点坐标  
CGContextStrokePath(context);

11.画三次贝塞尔曲线

CGContextMoveToPoint(context, 200, 300);//设置Path的起点  
CGContextAddCurveToPoint(context,250, 280, 250, 400, 280, 300);//设置贝塞尔曲线的控制点坐标和控制点坐标终点坐标  
CGContextStrokePath(context);

12.画文字

// 设置文字的属性
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSForegroundColorAttributeName] = [UIColor whiteColor];
dict[NSFontAttributeName] = [UIFont systemFontOfSize:14];
[@"I Love iOS" drawInRect:rect withAttributes:dict];

13.画图片

UIImage *image = [UIImage imageNamed:@"apple.jpg"];  
[image drawInRect:CGRectMake(60, 340, 20, 20)];//在坐标中画出图片  
//[image drawAtPoint:CGPointMake(100, 340)];//保持图片大小在point点开始画图片,可以把注释去掉看看  
CGContextDrawImage(context, CGRectMake(100, 340, 20, 20), image.CGImage);//使用这个使图片上下颠倒了      
//CGContextDrawTiledImage(context, CGRectMake(0, 0, 20, 20), image.CGImage);//平铺图

以上就是关于“iOS开发CGContextRef画图怎么使用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI