Quartz 2D简单的绘图

本文详细介绍如何使用Core Graphics框架进行绘图操作,包括线条、形状、文本及图像的绘制方法。文章通过实例展示了不同绘图技巧,如路径创建、属性设置等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//利用CG框架绘图的步骤:

1.子类化UIView

2.在GraphicView中复写drawRect方法

3.获取图形上下文对象

4.绘制线条

        (1)先创建一个路径,把线条定义在这个路径上,再把路径交给context去显示。

        (2)直接把线条绘制到context上去。

5.绘制简单图形

6.绘制文本

7.绘制图像

- (void)drawRect:(CGRect)rect {

        //1.获取图形上下文

        CGContextRef context = UIGraphicsGetCurrentContext();//图形上下文

        [self drawImage:context];

}

//绘制线条方法一

//创建一个绘制路径

CGMutablePathRef path =CGPathCreateMutable();

//2.在路径上添加一个起点

CGPathMoveToPoint(path,NULL, 50, 50);

CGPathAddLineToPoint(path,NULL, 100, 100);

//绘制完毕后关闭路径,这样会把起始点和终点连接

CGPathCloseSubpath(path);

//3.把路径添加到图形上下文

CGContextAddPath(context,path);

//4.设置图形上下文的属性

//设置线宽

CGContextSetLineWidth(context, 5);

//设置线条颜色

CGContextSetRGBStrokeColor(context, 254.0/255, 201.0/255, 21.0/255, 1);

//填充颜色

CGContextSetRGBFillColor(context, 0, 1, 0, 1);

//5.在图形上下文上绘制路径

/*

     绘制模式:

     kCGPathFill:填充(实心)

     kCGPathStroke:画线(空心)

     kCGPathFillStroke :即画线又填充

*/

CGContextDrawPath(context,kCGPathFillStroke);

//6.释放路径,有create就要有release

CGPathRelease(path);

//绘制线条方法二

CGPoint points[] = {{50,50},{100,100},{50,100},{50,50}};

CGContextAddLines(context,points, 4);

//设置线条颜色

[[UIColor redColor]setStroke];

//设置填充颜色

[[UIColor blueColor]setFill];

//同时设置线条和填充颜色

[[UIColor redColor] set];

CGContextDrawPath(context, kCGPathFillStroke);

//绘制矩形

//1.使用CG提供的函数

CGRect rect =CGRectMake(10, 10, 100, 200);

//添加到图形上下文中

CGContextAddRect(context,rect);

//设置context的属性

[[UIColor redColor]set];   

CGContextDrawPath(context,kCGPathFillStroke);

//2.使用UIKit框架提供的函数

CGRect rect =CGRectMake(10, 10, 100, 200);

//设置context的属性

[[UIColor redColor] set];

  

//绘制实心矩形

//UIRectFill(rect);

//绘制空心矩形

UIRectFrame(rect);

//绘制一个圆形

x,y圆心的坐标

radius:半径

startAngel:起始角度

endAngel:结束的角度

clockwise:0顺时针,1逆时针

CGContextAddArc(context,160, 160, 100, 0, M_PI_4, 0);

//2.给定一个矩形,绘制一个内切圆

CGRect rect =CGRectMake(50, 50, 100, 100);

[[UIColor redColor] set];

UIRectFrame(rect);

CGContextAddEllipseInRect(context, rect);

CGContextDrawPath(context,kCGPathFillStroke);

//绘制贝赛尔曲线

//设置起点

CGContextMoveToPoint(context, 20, 200);

//CGContextAddCurveToPoint(context, 100, 20, 200, 300, 300, 50);

CGContextAddQuadCurveToPoint(context, 150, 20, 300, 200);

[[UIColor redColor] set];

CGContextDrawPath(context,kCGPathStroke);

//绘制一个文本

NSString *str =@"hello world";

CGRect rect =CGRectMake(50, 50, 200, 300);

[[UIColor whiteColor]setFill];

UIRectFill(rect);

UIFont *font = [UIFontsystemFontOfSize:20];

UIColor *color = [UIColorredColor];

 //创建段落样式对象,通过对其属性的设置可以设置文字在段落中显示的样式。

NSMutableParagraphStyle*style = [[NSMutableParagraphStyle alloc] init];

style.lineBreakMode =NSLineBreakByWordWrapping;

style.alignment =NSTextAlignmentCenter;  

//    [str drawInRect:rectwithFont:font lineBreakMode:NSLineBreakByWordWrappingalignment:NSTextAlignmentCenter];

[str drawInRect:rectwithAttributes:

        @{

        NSFontAttributeName:font,

        NSForegroundColorAttributeName:color,

        NSParagraphStyleAttributeName:style

}];

//这种绘制文本的方式非常轻量级,不需要创建一个UILabel控件,并且可以根据需求定制效果。

//绘制图片

//1.使用UIKit提供的方法绘制

UIImage *image = [UIImageimageNamed:@"mp3.jpg"];

//图片拉伸在指定矩形区域显示

[imagedrawInRect:CGRectMake(0, 0, 320, 200)];

//图片平铺在指定区域显示

//[imagedrawAsPatternInRect:CGRectMake(0, 0, 320, 200)];

//2.用CoreGraphic提供的函数绘制

//CG坐标系统转化为UIKit的坐标系统

CGContextSaveGState(context);

//(1)顺时针旋转180度

CGContextRotateCTM(context, M_PI);

//(2)x缩放为原来的-1倍

CGContextScaleCTM(context,-1, 1);

//(3)向上平移

CGContextTranslateCTM(context, 0,-image.size.height);

CGContextDrawImage(context, CGRectMake(0,0, image.size.width, image.size.height), image.CGImage);

//(4)恢复context

CGContextRestoreGState(context);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值