iOS-CGContextRef
图形上下文(Graphics Context)---绘制目标
需要在iOS应用程序的屏幕上进行绘制时,需要先定义一个UIView类,并实现它的drawRect:方法,当启动程序时,会先调用loadView,然后是ViewDidLoad,接下来才是drawRect:方法。
1.画单条线
//1 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//2 创建一个绘制的路径
CGMutablePathRef path = CGPathCreateMutable();
//画线
//(1)设置起始点
CGPathMoveToPoint(path, NULL, 50, 50);
//(2)设置目标点
CGPathAddLineToPoint(path, NULL, 200, 200);
CGPathAddLineToPoint(path, NULL, 50, 200);
// CGPathAddLineToPoint(path, NULL, 50, 50);
//关闭路径(将路径封闭起来)
CGPathCloseSubpath(path);
//3 将路径添加到上下文
CGContextAddPath(context, path);
//4 设置上下文的属性
//设置填充颜色
CGContextSetRGBFillColor(context, 250/255.0, 200/255.0, 50/255.0, 1.0);
//设置线条颜色
CGContextSetRGBStrokeColor(context, 65/255.0, 170/255.0, 50/255.0, 1.0);
//设置线条宽度
CGContextSetLineWidth(context, 30);
//设置线条转折点的样式
CGContextSetLineJoin(context, kCGLineJoinRound);
//5 绘制路径
/*绘制模式:
kCGPathFill:填充(实心)
kCGPathStroke:只画线(空心)
kCGPathFillStroke:即画线又填充
*/
CGContextDrawPath(context, kCGPathFillStroke);
//6 释放路径
CGPathRelease(path);
2.画多条线
//1 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//2 添加多条线
CGPoint p0 = {50,50};
CGPoint p1 = {200,200};
CGPoint p2= {50,200};
CGPoint p3 = {50,50};
CGPoint points[] = {p0,p1,p2,p3};
CGContextAddLines(context, points, 4);
//3 设置属性
// CGContextSetRGBStrokeColor(context, 65/255.0, 170/255.0, 50/255.0, 1.0);
//---UIKit--------
//设置线条颜色
[[UIColor redColor] setStroke];
//设置填充颜色
[[UIColor blueColor]setFill];
//4 绘制路径
CGContextDrawPath(context, kCGPathFillStroke);
3.画矩形
//----------第一种:core Graphics-----------
//1 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//2 添加矩形
// CGRect rect = CGRectMake(40, 40, 100, 200);
// CGContextAddRect(context, rect);
//
//
// //3 设置属性
// [[UIColor redColor]setStroke];
//// [[UIColor orangeColor]setFill];
//
//
// //4 绘制
// CGContextDrawPath(context, kCGPathFillStroke);
//----------第二种:UIKit- 提供绘制矩形的函数 (已经封装好的)----------
[[UIColor redColor]setStroke];
[[UIColor orangeColor]setFill];
CGRect rect = CGRectMake(40, 40, 100, 200);
//绘制线条矩形(空心)
// UIRectFrame(rect);
//绘制填充的矩形(实心)
UIRectFill(rect);
4.画弧线
//1 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//-----------第一种: 绘制圆弧------------
/**
*
*
* @param context 上下文
* @param x#> 圆的中心点坐标x description#>
* @param y#> 圆的中心点坐标y description#>
* @param radius#> 圆的半径 description#>
* @param startAngle#> 开始的角度 description#>
* @param endAngle#> 结束的角度 description#>
* @param clockwise#> 画的方向 0 顺时针 1 逆时针
*
* @return <#return value description#>
*/
CGContextAddArc(context, 160, 100, 100, 0, M_PI_4, 0);
//设置属性
[[UIColor redColor]setStroke];
[[UIColor orangeColor]setFill];
//绘制
CGContextDrawPath(context, kCGPathFillStroke);
//-----------第二种: 绘制内切椭圆------------
CGRect rect = CGRectMake(50, 50, 200, 100);
//设置属性
[[UIColor blackColor]setStroke];
[[UIColor orangeColor]setFill];
//绘制线条矩形
UIRectFrame(rect);
//根据矩形绘制的椭圆
CGContextAddEllipseInRect(context, rect);
//绘制
CGContextDrawPath(context, kCGPathFillStroke);
5.画曲线
//1 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//1.设置起点
CGContextMoveToPoint(context, 20, 200);
//2 画贝塞尔曲线
//(1) 3个点
/*
<#CGContextRef _Nullable c#>:上下文
cp1x cp1y: 第一条切线的终点
cp2x cp2y: 第二条切线的起点
x y: 第二条切线的终点
*/
// CGContextAddCurveToPoint(context, 100, 20, 200, 300, 300, 50);
// //3 设置属性
// [[UIColor redColor] setStroke];
//
// //4 绘制
// CGContextDrawPath(context, kCGPathStroke);
//(2) 2个点
[[UIColor redColor] setStroke];
CGContextAddQuadCurveToPoint(context, 150, 20, 300, 200);
CGContextDrawPath(context, kCGPathStroke);
6.画文字
//1 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(50, 50, 200, 50);
//绘制矩形
UIRectFrame(rect);
// UIFont *font = [UIFont systemFontOfSize:20];
/*lineBreakMode:换行的方式
NSLineBreakByWordWrapping:根据单词换行
NSLineBreakByCharWrapping:根据字符换行
*/
//绘制文字
// [str drawInRect:rect withFont:font lineBreakMode:NSLineBreakByCharWrapping alignment:NSTextAlignmentCenter];
//2 新方法:
//属性字典
NSDictionary *dic = @{
NSFontAttributeName:[UIFont systemFontOfSize:20],
NSBackgroundColorAttributeName:[UIColor greenColor],
NSForegroundColorAttributeName:[UIColor whiteColor]
};
[str drawInRect:rect withAttributes:dic];
7.画图
//1 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage *image = [UIImage imageNamed:@"37.jpg"];
//----UKit-------
//1.指定一个点来绘制图片(锚点)
// [image drawAtPoint:CGPointMake(50, 50)];
// //2.制定一个矩形范围来绘制(拉伸填充)
// [image drawInRect:CGRectMake(0, 0, 200, 300)];
// //3.指定一个矩形范围平铺绘制
// [image drawAsPatternInRect:CGRectMake(0, 0, 200, 300)];
//
//二 core graphics
//1 保存上下文状态
CGContextSaveGState(context);
//2 切换坐标系
// Quartz 2D的坐标系----->UIKit的坐标系
//(1)向上平移一个高度
CGContextTranslateCTM(context, 0, 200);
//(2)改变Y轴的方向
CGContextScaleCTM(context, 1, -1);
//3 图片绘制
CGContextDrawImage(context, CGRectMake(0, 0, 300, 200), image.CGImage);
//4 恢复到之前保存的上下文状态
CGContextRestoreGState(context);
iOS-CGContextRef的更多相关文章
- iOS CGContextRef 画一条直线,仅仅是画一条直线
今天周末休息,想好好补补课,无奈,弄了一上午,全部都是半边拉块的demo,有一种深深的挫败感. 中午睡醒一觉后,又看了一集“奔跑吧兄弟”,然后一下午时间就过去了. 仔细一想,应该是我的补课方法不对:要 ...
- iOS CGContextRef画图时的常用方法
UIView的drawRect方法 CoreGraphics绘图 综述:描述系统会调用UIView的drawRect方法,所以coreGraphics的所有实现代码放在该函数内,setNeedsDis ...
- iOS CGContextRef 画图小结
CGContextRef context = UIGraphicsGetCurrentContext(); //设置上下文 //画一条线 CGContextSetStrokeColorWithColo ...
- iOS CGContextRef/UIBezierPath(绘图)
绘图的底层实现方法 注意:在drawRect方法中系统会默认创建一个上下文(C语言类型)在其他方法中不会有这样一个上下文(可以自己测试) @implementation DrawView //注意,在 ...
- CoreText
[CoreText] Core Text is designed for development of higher-level text-handling frameworks. General ...
- iOS: 使用CGContextRef,CGPath和UIBezierPath来绘画
这三种东西:CGContextRef,CGPath和UIBezierPath.本质上都是一样的,都是使用Quartz来绘画.只不过把绘图操作暴露在不同的API层面上,在具体实现上,当然也会有一些细小的 ...
- IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
... 首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Con ...
- (转) IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...
- [ios]ios画线 使用CGContextRef,CGPath和UIBezierPath来绘画
参考 :http://www.mgenware.com/blog/?p=493 这三种东西:CGContextRef,CGPath和UIBezierPath.本质上都是一样的,都是使用Quartz来绘 ...
- [置顶] IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...
随机推荐
- Django之密码加密
通过django自带的类库,来加密解密很方便,下面来简单介绍下: 导入包: from django.contrib.auth.hashers import make_password, check_p ...
- ASP.NET(C#)图片加文字、图片水印,神啊,看看吧
ASP.NET(C#)图片加文字.图片水印 一.图片上加文字: //using System.Drawing; //using System.IO; //using System.Drawing.Im ...
- 【Leetcode_easy】1128. Number of Equivalent Domino Pairs
problem 1128. Number of Equivalent Domino Pairs solution1: 不明白为什么每个元素都要加上count: class Solution { pub ...
- 安卓微信overflow-x overflow-y引发的bug
今天xgo文章图片页上线用微信扫页面发现一个bug,页面可以双击放大缩小. 找了半天原因,发现是图片描述设置了overflow-y引发的bug. 建议在微信场景里满屏显示不能滚动的页面里慎用overf ...
- 反馈神经网络Hopfield网络
一.前言 经过一段时间的积累,对于神经网络,已经基本掌握了感知器.BP算法及其改进.AdaLine等最为简单和基础的前馈型神经网络知识,下面开启的是基于反馈型的神经网络Hopfiled神经网络.前馈型 ...
- Meerkat软件
一.准备工作 meerkat 0.189版本和以前的版本相比,支持bwa mem 输出的bam文件,还支持全外显子数据count SV. meerkat原理 1.1 需要准备的软件 unix/Linu ...
- PyCharm:设置py文件头部信息
P PyCharm:设置py文件头部信息file->setting->appearance & behavior->editor->file and code temp ...
- Android studio 3.1.3真机调试报错,no target device found
Android studio 3.1.2 的 Android monitor 改为 Android profiler,直接点这个就可以真机调试,在手机安装相应app 如果不行,报错,"no ...
- c++实现的顺序栈
栈是一种运算受限的线性表,是一种先进后出的数据结构,限定只能在一端进行插入和删除操作,允许操作的一端称为栈顶,不允许操作的称为栈底 因此需要的成员变量如下 int *_stack; //指向申请的空间 ...
- MySql五大引擎的区别以及优劣之分
五大引擎 一: MyISAM:在创建MyISAM的时候会出来三个默认的文件 1.tb_demo.frm,存储表定义:2.tb_demo.MYD,存储数据:3.tb_demo.MYI,存储索引. 因为M ...