图形上下文(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的坐标系-----&gt;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的更多相关文章

  1. iOS CGContextRef 画一条直线,仅仅是画一条直线

    今天周末休息,想好好补补课,无奈,弄了一上午,全部都是半边拉块的demo,有一种深深的挫败感. 中午睡醒一觉后,又看了一集“奔跑吧兄弟”,然后一下午时间就过去了. 仔细一想,应该是我的补课方法不对:要 ...

  2. iOS CGContextRef画图时的常用方法

    UIView的drawRect方法 CoreGraphics绘图 综述:描述系统会调用UIView的drawRect方法,所以coreGraphics的所有实现代码放在该函数内,setNeedsDis ...

  3. iOS CGContextRef 画图小结

    CGContextRef context = UIGraphicsGetCurrentContext(); //设置上下文 //画一条线 CGContextSetStrokeColorWithColo ...

  4. iOS CGContextRef/UIBezierPath(绘图)

    绘图的底层实现方法 注意:在drawRect方法中系统会默认创建一个上下文(C语言类型)在其他方法中不会有这样一个上下文(可以自己测试) @implementation DrawView //注意,在 ...

  5. CoreText

    [CoreText]  Core Text is designed for development of higher-level text-handling frameworks. General ...

  6. iOS: 使用CGContextRef,CGPath和UIBezierPath来绘画

    这三种东西:CGContextRef,CGPath和UIBezierPath.本质上都是一样的,都是使用Quartz来绘画.只不过把绘图操作暴露在不同的API层面上,在具体实现上,当然也会有一些细小的 ...

  7. IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)

    ... 首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Con ...

  8. (转) IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)

    首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...

  9. [ios]ios画线 使用CGContextRef,CGPath和UIBezierPath来绘画

    参考 :http://www.mgenware.com/blog/?p=493 这三种东西:CGContextRef,CGPath和UIBezierPath.本质上都是一样的,都是使用Quartz来绘 ...

  10. [置顶] IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)

    首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...

随机推荐

  1. 【Redis】Redis 主从模式搭建

    主从模式介绍 Redis虽然读取写入的速度都特别快,但是也会产生读压力特别大的情况.为了分担读压力,Redis支持主从复制,Redis的主从结构可以采用一主多从或者级联结构,Redis主从复制可以根据 ...

  2. Python - Django - FBV 和 CBV

    FBV: Function Base View,基于函数的视图 views.py: from django.shortcuts import render, HttpResponse # FBV de ...

  3. LeetCode_374. Guess Number Higher or Lower

    374. Guess Number Higher or Lower Easy We are playing the Guess Game. The game is as follows: I pick ...

  4. LeetCode_299. Bulls and Cows

    299. Bulls and Cows Easy You are playing the following Bulls and Cows game with your friend: You wri ...

  5. Swift4.0复习枚举

    1.枚举类型: “Swift编程语言中,枚举类型属于值类型,而不是引用类型.” 摘录来自: “大话Swift 4.0”. iBooks. 2.枚举类型和枚举对象的定义: enum Enumeratio ...

  6. 【Leetcode_easy】1051. Height Checker

    problem 1051. Height Checker solution class Solution { public: int heightChecker(vector<int>&a ...

  7. 转录组组装软件stringtie

    StringTie是約翰·霍普金斯大學计算机生物中心开发的一款转录组组装软件,在组装转录本的完整度,精度和速度方面都较以往的cufflinks 有很大的提升,也是目前有参考基因组转录组主流的组装软件. ...

  8. Linux 脚本语言入门

    0.脚本编写初步介绍 (1)脚本第一行以 #!/bin/sh 开始,也可以用 #!/bin/bash 开始,但是第一行必须以这种方式开始. (2)脚本名需要以.sh结尾 (3)#开头的句子表示注释 ( ...

  9. springboot之Redis

    1.springboot之Redis配置 在学习springboot配置Redis之前先了解Redis. 1.了解Redis Redis简介: redis是一个key-value存储系统.和Memca ...

  10. http GET 和 POST 请求的优缺点、区别以及误区

    原文章:https://blog.csdn.net/qq_28483283/article/details/80207674 请优先参考原文章 Get和Post在面试中一般都会问到,一般的区别: (1 ...