iOS 画图讲解

时间:2025-03-17 18:36:01

5、画图

(1)画线

//绘图代码写在drawRect里,view加载完成,需要显示的时候调用

//1、获取图形上下文 2、创建路径 3、把图形放入上下文 4、渲染上下文

//drawRect的rect是绘制View的bounds

//重绘

[self setNeedsDisplay];

set =  setStroke(描边) + setFill(填充)

//方法一:最原始的方法

//1、获取图形上下文,CG,CoreGraphics,有关图形的框架,开发mac也可以用

CGContextRef ctx = UIGraphicsGetCurrentContext();

//2、创建路径

CGMutablePathRef path = CGPathCreateMutable();

//设置起始点

CGPathMoveToPoint(path, NULL, 50, 50);

//画线到某一点

CGPathAddLineToPoint(path, NULL, 200, 200);

//3、把图形放入上下文

CGContextAddPath(ctx, path);

//4、渲染上下文

CGContextStrokePath(ctx);

//绘图的第二种方法

- (void)drawLine2 {

CGContextRef ctx = UIGraphicsGetCurrentContext();

//设置起始点

CGContextMoveToPoint(ctx, 50, 50);

//画线并设置终点

CGContextAddLineToPoint(ctx, 200, 200);

//渲染上下文

CGContextStrokePath(ctx);

}

//第三种方法

- (void)drawLine3 {

//贝塞尔线,UIKit

//创建路径

UIBezierPath * path = [UIBezierPath bezierPath];

//设置起点

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

//画线并设置终点

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

//绘制路径

[path stroke];

}

//属性

- (void)drawCtxState {

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextMoveToPoint(ctx, 50, 50);

CGContextAddLineToPoint(ctx, 100, 50);

//    CGContextMoveToPoint(ctx, 80, 60);

//默认下一根线的起点就是上一根线的终点

CGContextAddLineToPoint(ctx, 100, 200);

//设置线宽度

CGContextSetLineWidth(ctx, 5);

//设置连接样式

CGContextSetLineJoin(ctx, kCGLineJoinRound);

//设置顶角样式

CGContextSetLineCap(ctx, kCGLineCapRound);

//设置线的颜色

[[UIColor redColor] setStroke];

CGContextStrokePath(ctx);

}

//画曲线

- (void)drawRect:(CGRect)rect {

// Drawing code

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextMoveToPoint(ctx, 50, 50);

//画曲线 arg1 上下文  arg2,3 控制点x,y   arg4,5 终点x,y

CGContextAddQuadCurveToPoint(ctx, 100, 100, 250, 50);

CGContextStrokePath(ctx);

}

(2)画图形

//画矩形

UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 200, 200)];

//画圆角矩形

UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 200, 200) cornerRadius:100];

//画圆弧

//Center 圆弧中心

//radius 圆弧半径

//startAngle 起始角度

//endAngle 结束角度

//clockwise YES 顺时针  NO 逆时针

CGPoint center = CGPointMake(125, 125);

UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:M_PI_2 clockwise:NO];

[path addLineToPoint:center];

//封闭路径

[path closePath];

[path stroke];

//要使用setFill,路径一定是封闭的

[[UIColor greenColor] setFill];

[path fill];

//画椭圆

UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 50, 200, 100)];

[path stroke];

}

6、绘制图片和文字

(1)绘制图片

- (void)drawPicture {

//图片裁剪,超出部分全部剪掉

UIRectClip(CGRectMake(0, 0, 50, 50));

UIImage * image = [UIImage imageNamed:@"01"];

//根据rect拉伸图片

[image drawInRect:CGRectMake(0, 0, 100, 100)];

[image drawInRect:rect];

//显示原图片尺寸

[image drawAtPoint:CGPointZero];

//平铺绘图

[image drawAsPatternInRect:rect];

}

(2)绘制文字

- (void)drawRect:(CGRect)rect {

// Drawing code

NSString * string = @"hgfdagskjhdcadkhdkjlashkdklhahgfdagskjhdcad";

NSShadow * shadow = [[NSShadow alloc] init];

shadow.shadowColor = [UIColor yellowColor];

shadow.shadowOffset = CGSizeMake(5, 5);

//模糊度

shadow.shadowBlurRadius = 3;

NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:30],

NSForegroundColorAttributeName:[UIColor redColor],

NSStrokeColorAttributeName:[UIColor blueColor],

NSStrokeWidthAttributeName:@(2),

NSShadowAttributeName: shadow};

//不会换行

[string drawAtPoint:CGPointZero withAttributes:nil];

[string drawInRect:rect withAttributes:dict];

}

5、画图

(1)画线

//绘图代码写在drawRect里,view加载完成,需要显示的时候调用

//1、获取图形上下文 2、创建路径 3、把图形放入上下文 4、渲染上下文

//drawRect的rect是绘制View的bounds

//重绘

[self setNeedsDisplay];

set =  setStroke(描边) + setFill(填充)

//方法一:最原始的方法

//1、获取图形上下文,CG,CoreGraphics,有关图形的框架,开发mac也可以用

CGContextRef ctx = UIGraphicsGetCurrentContext();

//2、创建路径

CGMutablePathRef path = CGPathCreateMutable();

//设置起始点

CGPathMoveToPoint(path, NULL, 50, 50);

//画线到某一点

CGPathAddLineToPoint(path, NULL, 200, 200);

//3、把图形放入上下文

CGContextAddPath(ctx, path);

//4、渲染上下文

CGContextStrokePath(ctx);

//绘图的第二种方法

- (void)drawLine2 {

CGContextRef ctx = UIGraphicsGetCurrentContext();

//设置起始点

CGContextMoveToPoint(ctx, 50, 50);

//画线并设置终点

CGContextAddLineToPoint(ctx, 200, 200);

//渲染上下文

CGContextStrokePath(ctx);

}

//第三种方法

- (void)drawLine3 {

//贝塞尔线,UIKit

//创建路径

UIBezierPath * path = [UIBezierPath bezierPath];

//设置起点

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

//画线并设置终点

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

//绘制路径

[path stroke];

}

//属性

- (void)drawCtxState {

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextMoveToPoint(ctx, 50, 50);

CGContextAddLineToPoint(ctx, 100, 50);

//    CGContextMoveToPoint(ctx, 80, 60);

//默认下一根线的起点就是上一根线的终点

CGContextAddLineToPoint(ctx, 100, 200);

//设置线宽度

CGContextSetLineWidth(ctx, 5);

//设置连接样式

CGContextSetLineJoin(ctx, kCGLineJoinRound);

//设置顶角样式

CGContextSetLineCap(ctx, kCGLineCapRound);

//设置线的颜色

[[UIColor redColor] setStroke];

CGContextStrokePath(ctx);

}

//画曲线

- (void)drawRect:(CGRect)rect {

// Drawing code

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextMoveToPoint(ctx, 50, 50);

//画曲线 arg1 上下文  arg2,3 控制点x,y   arg4,5 终点x,y

CGContextAddQuadCurveToPoint(ctx, 100, 100, 250, 50);

CGContextStrokePath(ctx);

}

(2)画图形

//画矩形

UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 200, 200)];

//画圆角矩形

UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 200, 200) cornerRadius:100];

//画圆弧

//Center 圆弧中心

//radius 圆弧半径

//startAngle 起始角度

//endAngle 结束角度

//clockwise YES 顺时针  NO 逆时针

CGPoint center = CGPointMake(125, 125);

UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:M_PI_2 clockwise:NO];

[path addLineToPoint:center];

//封闭路径

[path closePath];

[path stroke];

//要使用setFill,路径一定是封闭的

[[UIColor greenColor] setFill];

[path fill];

//画椭圆

UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 50, 200, 100)];

[path stroke];

}

6、绘制图片和文字

(1)绘制图片

- (void)drawPicture {

//图片裁剪,超出部分全部剪掉

UIRectClip(CGRectMake(0, 0, 50, 50));

UIImage * image = [UIImage imageNamed:@"01"];

//根据rect拉伸图片

[image drawInRect:CGRectMake(0, 0, 100, 100)];

[image drawInRect:rect];

//显示原图片尺寸

[image drawAtPoint:CGPointZero];

//平铺绘图

[image drawAsPatternInRect:rect];

}

(2)绘制文字

- (void)drawRect:(CGRect)rect {

// Drawing code

NSString * string = @"hgfdagskjhdcadkhdkjlashkdklhahgfdagskjhdcad";

NSShadow * shadow = [[NSShadow alloc] init];

shadow.shadowColor = [UIColor yellowColor];

shadow.shadowOffset = CGSizeMake(5, 5);

//模糊度

shadow.shadowBlurRadius = 3;

NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:30],

NSForegroundColorAttributeName:[UIColor redColor],

NSStrokeColorAttributeName:[UIColor blueColor],

NSStrokeWidthAttributeName:@(2),

NSShadowAttributeName: shadow};

//不会换行

[string drawAtPoint:CGPointZero withAttributes:nil];

[string drawInRect:rect withAttributes:dict];

}