IOS开发之Quartz2D绘图的使用

/*

*绘图:Quarz 2D  ——》 Core Graphics

作用:

绘制图形 : 线条\三角形\矩形\圆\弧等

绘制文字 绘制\生成图片(图像)

读取\生成PDF

截图\裁剪图片

自定义UI控件

涂鸦\画板

手势解锁 … …

注意:

Quartz2D的API是纯C语言的

Quartz2D的API来自于Core Graphics框架

需要导入

CoreGraphics.framework数据类型和函数基本都以CG作为前缀CGContextRefCGPathRefCGContextStrokePath(ctx);

步骤:

(1)CGContextRef  上下文 ——》 画板

(2)画图的内容 ——》 设置画图的内容

(3)把内容添加到上下文《画板》

(4)把内容画到画板上

常用方法介绍

(1) CGContextRef  上下文

(2) 路径

《1》UIBezierPath

《2》CGMutablePathRef   通过一个点绘制一个路径

《3》注意  必须设置起始点CGContentextMoveToPoint

(3) 画图形

矩形 CGContextAddRect

曲线 CGContextAddCurveToPoint

圆形 CGContextAddEllipseInRect

(3.1) CGContextSetLineWidth 设置笔画宽度

(3.2)set 设置笔画的颜色

(3.3)setFill 划线区域范围的填充

(3.4)setStroke 设置笔画的颜色

(3.5)设置画笔填充样式

《1》kCGPathFill 只填充

《2》kCGPathStroke 画笔颜色

《3》kCGPathFillStroke 既填充又有画笔颜色

(4)截图

(1)UIGraphicsBeginImageContextWithOptions开始截图

(2)获得当前图片上下文的图片UIGraphicsGetImageFromCurrentImageContext()

(3)UIGraphicsEndImageContext 关闭图片上下文

(4)UIGraphicsBeginImageContext开始获得图片上下文

(5)CGContextStrokePath 把路径绘制到图片上下文

(6)把路径绘制到界面上面  直接stroke

*/

// 所有画图的代码写在 drawRect 里面

//1. 初始化的时候会调用

//2。 setNeedsDisplay

- (void)drawRect:(CGRect)rect{

[super drawRect:rect];

//    NSLog(@"dsadsadsa");

[self addline];

}

// 画直线

- (void)addline{

//1.创建画布 上下文

//   获得当前的视图作为上下文

CGContextRef context = UIGraphicsGetCurrentContext();

/*

point  中心点

*/

//2.创建画图的内容

//    *必须有一个起始点

//    *还要有个移动的位置(另一个位置)

UIBezierPath *path = [UIBezierPath bezierPath];

//    2.1 设置其他点

[path addLineToPoint:CGPointMake(50, 50)];

[path addLineToPoint:CGPointMake(100, 100)];

[path addLineToPoint:CGPointMake(300, 300)];

//    2.2 设置画笔的宽度

path.lineWidth = 2;

//    2.3 设置画笔的颜色

//    [[UIColor redColor]set];

//    设置画笔颜色

[[UIColor whiteColor] setStroke];

//    设置填充颜色

[[UIColor brownColor] setFill];

//3. 把画上的路径添加到上下文

CGContextAddPath(context, path.CGPath);

//4. 把画上的内容添加到画板

//    CGContextStrokePath(context);

//    设置填充的样式

CGContextDrawPath(context, kCGPathFillStroke);

}

//画直线

- (void)addLine{

//    1、创建 画布 上下文

//    获得当前的上下文 当做画布

CGContextRef context =  UIGraphicsGetCurrentContext();

//    2、创建画图的内容

UIBezierPath *path = [UIBezierPath bezierPath];

/*

Point 中心点

x 中心点x

y 中心点y

y不变 x从小值 - 大值 横向直线

*/

//    2.1设置起始点

[path moveToPoint:CGPointMake(10, 50)];

//    2.2 添加其他点

[path addLineToPoint:CGPointMake(10, 500)];

[path addLineToPoint:CGPointMake(300, 500)];

//    2.3设置画笔宽度

path.lineWidth = 2;

//    2.4设置画笔颜色

//    [[UIColor whiteColor] set];

//    设置画笔颜色

[[UIColor whiteColor] setStroke];

//    设置填充颜色

[[UIColor brownColor] setFill];

//    3、把画的内容《路径》添加到上下文《画布》

CGContextAddPath(context, path.CGPath);

//    4、绘制 渲染 内容到 上下文 《画布》

//    CGContextStrokePath(context);

//    设置填充的样式

CGContextDrawPath(context, kCGPathFillStroke);

}

//添加矩形

- (void)addRect{

//   1.画布

CGContextRef context = UIGraphicsGetCurrentContext();

//   2.内容

CGContextAddRect(context, CGRectMake(0, 0, 100, 100));

[[UIColor redColor]set];

//    设置画笔的宽度

CGContextSetLineWidth(context, 3);

//   3.渲染

//    直接渲染矩形

//    CGContextStrokeRect(context, CGRectMake(0, 0, 100, 100));

CGContextDrawPath(context, kCGPathFillStroke);

}

//  画圆形

- (void)addRound{

//    画布

content8 = UIGraphicsGetCurrentContext();

//    内容

CGContextAddEllipseInRect(content8, CGRectMake(10, 10, 100, 100));

[[UIColor blackColor]set];

//    渲染到画布

CGContextDrawPath(content8, kCGPathFillStroke);

}

// 画曲线

- (void)addCurve{

//   画布

CGContextRef contenxt = UIGraphicsGetCurrentContext();

//    内容

UIBezierPath *path = [UIBezierPath bezierPath];

[path moveToPoint:CGPointMake(100, 100)];

//    [path addCurveToPoint:CGPointMake(200, 150) controlPoint1:CGPointMake(0, 0) controlPoint2:CGPointMake(200, 150)];

/**

*  画圆

*

*  CGPointMake 中心点

*  radius      半径

*  startAngle   开始的弧度

*  endAngle     结束的弧度

*  clockwise    是顺时针还是逆时针

*/

[path addArcWithCenter:CGPointMake(200, 200) radius:100 startAngle:M_PI_2 endAngle:M_PI clockwise:YES];

[[UIColor redColor]setStroke];

[[UIColor blackColor]setFill];

//    把内容添加到画布上

CGContextAddPath(contenxt, path.CGPath);

//  渲染

CGContextDrawPath(contenxt, kCGPathFillStroke);

}

// 画线简化

- (void)addLine2{

//  1.路径

//  2。画出内容

UIBezierPath *path = [UIBezierPath bezierPath];

[path moveToPoint:CGPointMake(100, 100)];

[path addLineToPoint:CGPointMake(200, 500)];

[[UIColor redColor]set];

[path stroke];

}

//截屏

- (void)cutScreen{

//    1.获得一个图片的上下文

//    2.画布的上下文

//    3.截图

//    4.

//    5.关闭图片的上下文

//    6.保存

UIGraphicsBeginImageContext(self.frame.size);

[self addRound];

[self.layer renderInContext:content8];

/*

第一个参数  图片尺寸

第二个参数 是否不透明  yes不透明  no透明

第三个参数 比例

*/

UIGraphicsBeginImageContextWithOptions(self.frame.size, YES, 1);

//    开始截图

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

//    关闭截图上下文

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

}

时间: 2024-08-09 19:49:47

IOS开发之Quartz2D绘图的使用的相关文章

iOS开发之Quartz2D

1.         Quartz2D概述及作用 Quartz2D的API是纯C语言的,Quartz2D的API来自于Core Graphics框架. 数据类型和函数基本都以CG作为前缀,比如: CGContextRef CGPathRef CGContextStrokePath(ctx); …… Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统. Quartz 2D能完成的工作: 绘制图形 : 线条\三角形\矩形\圆\弧等: 绘制文字: 绘制\生成图片(图像): 读取\生成PD

iOS开发之Quartz2D 二:绘制直线,曲线,圆弧,矩形,椭圆,圆

#import "DrawView.h" @implementation DrawView /** * 作用:专门用来绘图 * 什么时候调用:当View显示的时候调用 * @param rect:当View的bounds */ - (void)drawRect:(CGRect)rect { // Drawing code // NSLog(@"%s",__func__); // NSLog(@"%@",NSStringFromCGRect(rec

iOS开发之Quartz2D 六 绘制UIImageView

#import <UIKit/UIKit.h> @interface XMGImageView : UIView /** <#注释#> */ @property (nonatomic, strong) UIImage *image; - (instancetype)initWithImage:(UIImage *)image; @end #import "XMGImageView.h" @implementation XMGImageView - (instan

IOS开发之copy的问题

copy的目的就是修改副本,修改原始对象和副本时不会产生干扰. 定义一个不可变属性A,再定义一个可变属性B.用B做添加删除等操作后再将B赋值给A时,有些人习惯用A = B:其实这样是不安全的. 假设有下面的一段代码: ? 1 2 3 4 5 6 7 8 9 10   int main() {    NSMutableString *strM = [NSMutableString [email protected]"123"];    NSString *str = strM;    N

iOS开发之WKWebView简单使用和常用使用场景

iOS开发之 WKWebVeiw使用 想用UIWebVeiw做的,但是突然想起来在iOS8中出了一个新的WKWebView,算是UIWebVeiw的升级版.本着对新事物的好奇,就上网查了一下,但是找了好多个都没说的多了详细,于是就问谷歌,找文档,看看使用方法,试用了一下,果然不错,记录下来,大家分享! WKWebView的特点: 性能高,稳定性好,占用的内存比较小, 支持JS交互 支持HTML5 新特性 可以添加进度条(然并卵,不好用,还是习惯第三方的). 支持内建手势, 据说高达60fps的刷

iOS开发之Auto Layout入门

随着iPhone6与iOS8的临近,适配的问题讲更加复杂,最近学习了一下Auto Layout的使用,与大家分享.  什么是Auto Layout? Auto Layout是iOS6发布后引入的一个全新的布局特性,其目的是弥补以往Autoresizing在布局方面的不足之处,以及未来面对更多尺寸适配时界面布局可以更好的适应. 为什么要用Auto Layout? Autolayout能解决不同屏幕(iPhone4,iPhone5,iPad...)之间的适配问题. 在iPhone4时代开发者只需要适

iOS开发之CocoaPods的使用

透明色:00ff00ff //设置柱状图的颜色                ColorSet cs = new ColorSet();                cs.Id = "colorset1"; #region 设置柱状图的颜色 待开发                    string strColor = oYAXIS.Color;                    switch (strColor)                    {           

iOS开发之UILabel

UILabel是iOS开发中常用的一个组件,主要用来显示内容. UILabel的主要使用如下: ? 1 2 3 4 5 6 7 8 9 10 /*尺寸*/ CGRect labelRect = CGRectMake(100, 100, 80, 40); /*初始化*/ UILabel *titleLabel = [[UILabel alloc] initWithFrame:labelRect]; /*一些属性的设置*/ titleLabel.font = [UIFont systemFontOf

iOS开发之多XIB之间相互关联

Xib link Xib 1.直接加载xib中的UIView 创建一个View1.xib, 随便设一个背景色,加一个标识UILabel, 这样好知道是这个view是哪一个view. 你可以在这个view上加作意的subview,我只是说明原理,所以这儿并没有加作何subview. 最终我的View1如下图: 由于View1会放到其它View上作为subview,所以这儿size是Freeform, Status Bar是:None. 将下面代码放到viewDidLoad中: &1这行代码就是加载