1,CoreGraphics框架
注:当系统调用drawRect:方法时,这个上下文,系统已经自动创建好了,用这条语句可以拿到这个系统已经创建的上下文。
CGContextRef tex = UIGraphicsGetCurrentContext();
设置状态
CGContextSetLineWidth(tex,10);//线宽
CGContextSetLineCap(tex,kCGLineCapRound);//头部样式
CGContextSetRGBStrokeColor(tex,1,0,0,1);//颜色
CGContextSetRGBFillColor(tex,0,1,0,1);画直线
第一种方式
CGContextMoveToPoint(tex,30,30);
CGContextAddLineToPoint(tex,100,100);
CGContextAddLineToPoint(tex,120,20);
CGContextClosePath(tex);
CGContextSetLineJoin(tex,kCGLineJoinBevel);
注:实际上 这种方式系统内部实现也是创建了一个path路径对象第二种方式
CGContextRef ctx= UIGraphicsGetCurrentContext();
CGPathRef path =CGPathCreateMutable();
CGPathMoveToPoint(path,NULL, <#CGFloat x#>, <#CGFloat y#>);
CGContextAddPath(ctx, path);
CGContextFillPath(tex);
CGPathRelease(path);
CFRelease(path);// 或者用这个core foundation框架里的方法
注:先创建一个路径对象,再把我们的做的东西缓存到这个路径,再把这个路径添加到上下文的缓存,上下文再把路径渲染到view中,画其它图形原理都是这样的。
这其中需要内存管理,如果有Create/copy/retain,都要相应在添加到上下文后进行release
画矩形
第一种
CGContextAddRect(tex,CGRectMake(20,20,160,100));
CGContextFillPath(tex);第二种 (等价于第一种)
CGContextFillRect(tex,CGRectMake(20, 20, 160, 100));
第三种,用UIKit的OC方法
UIRectFill(CGRectMake(120, 150, 160, 100));
注:画空心矩形时用stroke
画椭圆
CGContextAddEllipseInRect(tex,CGRectMake(20,20,160,100));
画弧
CGContextAddArc(tex,100,100,40,0,M_PI,0);
画文字
NSString *str =@”slfjsdgjas”;
[str drawAtPoint:CGPointMake(10,170)withAttributes:nil];画图片
UIImage *image = [UIImageimageNamed:@”图片名字”];
[image drawAtPoint:CGPointMake(0,0)];平铺图形
[imagedrawAsPatternInRect:CGRectMake(0,0,200,200)];
矩阵操作
CGContextScaleCTM(tex,0.5,0.5);
CGContextRotateCTM(tex,M_PI_2);
CGContextTranslateCTM(tex,0,100);裁减图片
- 先画一个裁减的模具,然后再画要被裁减的图片
CGContextAddEllipseInRect(tex,CGRectMake(100,100,100,100));
将当前上下文的形状裁减出来,以后画东西只能画到裁减的图形里面,超出的部分都不显示
CGContextClip(tex);
UIImage *image1 = [UIImageimageNamed:@”图片名字”];
[image1drawAtPoint:CGPointMake(100,100)];渲染
CGContextFillPath(tex);
- 先画一个裁减的模具,然后再画要被裁减的图片
2,UIKit框架,”贝塞尔”
- 画直线
// 1,创建一个贝塞尔曲线路径
UIBezierPath *path = [UIBezierPathbezierPath];
// 2,移动到起始点
[pathmoveToPoint:CGPointMake(30,30)];
// 3,添加一条线,起始点,目标点 [
pathaddLineToPoint:CGPointMake(140,140)];
// 3.1,再加一条线
[pathaddLineToPoint:CGPointMake(250,30)];
// 4,闭合路径,让路径从起始点和"最后一条线"的目标点连线
[pathclosePath];
// 5,画线或填充
// 画线
[pathstroke];
//填充
[pathfill];
- 画图像
// 没有用到贝塞尔曲线
// 1,拿到图像
UIImage *image = [UIImageimageNamed:@"001.jpg"];
// 2,画图像
// 2.1,会根据指定的区域做拉伸处理
[image drawInRect:rect];
// 2.2,在指定点绘制原图,没有做拉伸效果,point是左上角的点
[imagedrawAtPoint:CGPointMake(80,80)];
// 2.3,Pattern -平铺
[imagedrawAsPatternInRect:rect];
> 用贝塞尔曲线
(待补)
- 画文字
1,设置文字属性
//经常会使用字典来设置文本的属性,头文件在UIKit框架的第一个
// 1.1,设置字体及大小
UIFont *font = [UIFont systemFontOfSize:50]; // 设置字体大小
// Bradley Hand 表示字体类型
UIFont *font = [UIFontfontWithName:@"Bradley Hand"size:50];
// familyNames 数组类型,存放着全部系统中已安装字体的
NSLog(@"%@", [UIFontfamilyNames]);
// 1.2,设置字体阴影
//阴影
NSShadow *shadow = [[NSShadowalloc]init];
//阴影颜色
shadow.shadowColor = [UIColorgreenColor];
//阴影偏移
shadow.shadowOffset =CGSizeMake(-10, -10);
//模糊度
shadow.shadowBlurRadius =20.0;
// 1.3,所有的属性都存放在字典中
NSDictionary *dict = @{NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColorredColor], NSStrokeColorAttributeName: [UIColoryellowColor], NSStrokeWidthAttributeName:@(5), NSShadowAttributeName: shadow
};
2,字符串 方法画上去
// 2.1,在指定点绘字符串
[strdrawAtPoint:CGPointMake(0,0)withAttributes:dict];
// 2.2,在指定区域绘字符串 ,drawInRect会自动换行
[strdrawInRect:rectwithAttributes:dict];
// 2.3,NSStringDrawingUsesLineFragmentOrigin选项支持换行
[strdrawWithRect:rectoptions:NSStringDrawingUsesLineFragmentOriginattributes:dictcontext:nil];
//与 2.3 方法比较
//计算文本的bounds
CGRect r = [strboundingRectWithSize:rect.sizeoptions:NSStringDrawingUsesLineFragmentOriginattributes:dictcontext:nil];
- 画矩形
//绘制矩形
UIRectFill(CGRectMake(120,150,160,100));
> 用贝塞尔曲线
// 创建
UIBezierPath *path = [UIBezierPathbezierPathWithRect:CGRectMake(x, y, w, h)];
// 填充
[path fill];
(4),裁减图形
//如果画完图,再设置裁剪区域,无效
UIRectClip(CGRectMake(20,20,160,100));
> 用贝塞尔曲线
CGRect r =CGRectMake(-40.0, -40.0,80.0,80.0);
UIBezierPath *path = [UIBezierPathbezierPathWithOvalInRect:r];
//添加剪切区域
[pathaddClip];
- 画圆弧
// 创建
UIBezierPath *path = [UIBezierPathbezierPathWithArcCenter:cradius:rstartAngle:sAendAngle:eAclockwise:YES];
// 填充
[path fill];
- 画椭圆
UIBezierPath*path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(-30.0, -40.0,60.0,80.0)];
[path fill];