網站首頁 編程語言 正文
本文實例為大家匯總了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);//平鋪圖?
原文鏈接:https://blog.csdn.net/wgl_happy/article/details/77680059
相關推薦
- 2022-05-04 shell腳本配合zabbix實現tomcat的故障自愈功能_linux shell
- 2022-07-03 golang defer,func()閉包,panic ,recover,contex
- 2022-12-09 react?hooks?UI與業務邏輯分離必要性技術方案_React
- 2022-10-21 Python?NumPy教程之數組的基本操作詳解_python
- 2022-07-07 python中列表對象pop()方法的使用說明_python
- 2022-05-29 C++重載運算符你真的了解嗎_C 語言
- 2023-07-03 Redis 中 List(列表)類型的命令及詳解
- 2023-08-13 微信小程序底部導航欄最多只能顯示五個,解決辦法
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支