iOS 绘图
2019-06-25 本文已影响0人
Rumbles
1.Quart2D绘图 绘图(CGcontext)
绘制简单的直线
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, 14, 19);
CGContextAddLineToPoint(ctx, 45, 65);
CGContextStrokePath(ctx);
2.贝塞尔曲线
绘制简单的直线
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:(CGPoint){20,20}];
[linePath addLineToPoint:(CGPoint){160,160}];
[linePath addLineToPoint:(CGPoint){200,20}];
3. 贝塞尔曲线 与 CGcontext 结合
//使用贝塞尔曲线和图形上下文画图
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
[path addLineToPoint:CGPointMake(233, 69)];
//这个是C语言的写法,必须使用path.CGPath,否则无效
CGContextAddPath(ctx, path.CGPath);
CGContextStrokePath(ctx);
4.贝塞尔曲线放在layer上
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:(CGPoint){20,20}];
[linePath addLineToPoint:(CGPoint){160,160}];
[linePath addLineToPoint:(CGPoint){200,20}];
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame = CGRectMake(0, 0, 200, 200);
layer.position = (CGPoint){ScreenWidth/2,ScreenWidth/2};
layer.lineWidth = 5;
layer.strokeColor = [UIColor orangeColor].CGColor;
layer.path = linePath.CGPath;
layer.fillColor = nil; // 默认是黑色
layer.lineCap = kCALineCapRound;
[_disPlayView.layer addSublayer:layer];
5.将 (CGcontext) 绘制在image上面
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, 14, 19);
CGContextAddLineToPoint(ctx, 45, 65);
CGContextStrokePath(ctx);
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();