我是靠谱客的博主 可靠大门,最近开发中收集的这篇文章主要介绍iOS 画笔轨迹优化,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

iOS 画笔轨迹优化

一 、常规画法

- (void)draw_draw:(CGPoint *)points context:(CGContextRef)context count:(NSInteger)count {
CGContextFillPath(context);
CGContextAddLines(context, points, count);//添加线
CGContextDrawPath(context, kCGPathStroke); //根据坐标绘制路径
}

二、优化方式一

static CGPoint midPointForPoints(CGPoint p1, CGPoint p2) {
return CGPointMake((p1.x + p2.x) / 2.0, (p1.y + p2.y) / 2.0);
}
- (UIBezierPath *)draw_quadCurvedPathWithPoints:(NSArray *)points {
CGPoint currentPoint = [points[0] CGPointValue];
CGPoint previousPoint = currentPoint;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:currentPoint];
if (points.count == 2) {
CGPoint p2 = [points[1] CGPointValue];
[path addLineToPoint:p2];
return path;
}
NSInteger count = [points count];
for (int i = 1; i < count; i++) {
currentPoint = [points[i] CGPointValue];
CGPoint midPoint = midPointForPoints(previousPoint, currentPoint);
[path addQuadCurveToPoint:midPoint controlPoint:previousPoint];
previousPoint = currentPoint;
}
return path;
}

三、优化方式二

#define POINT(_INDEX_) [(NSValue *)[points objectAtIndex:_INDEX_] CGPointValue]
- (UIBezierPath *)draw_smoothedPathWithPoints:(NSArray *)pointsArray andGranularity:(NSInteger)granularity {
NSMutableArray *points = [pointsArray mutableCopy];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, YES);
UIBezierPath *smoothedPath = [UIBezierPath bezierPath];
// Add control points to make the math make sense
[points insertObject:[points objectAtIndex:0] atIndex:0];
[points addObject:[points lastObject]];
[smoothedPath moveToPoint:POINT(0)];
for (NSUInteger index = 1; index < points.count - 2; index++) {
CGPoint p0 = POINT(index - 1);
CGPoint p1 = POINT(index);
CGPoint p2 = POINT(index + 1);
CGPoint p3 = POINT(index + 2);
// now add n points starting at p1 + dx/dy up until p2 using Catmull-Rom splines
for (int i = 1; i < granularity; i++) {
float t = (float) i * (1.0f / (float) granularity);
float tt = t * t;
float ttt = tt * t;
CGPoint pi; // intermediate point
pi.x = 0.5 * (2*p1.x+(p2.x-p0.x)*t + (2*p0.x-5*p1.x+4*p2.x-p3.x)*tt + (3*p1.x-p0.x-3*p2.x+p3.x)*ttt);
pi.y = 0.5 * (2*p1.y+(p2.y-p0.y)*t + (2*p0.y-5*p1.y+4*p2.y-p3.y)*tt + (3*p1.y-p0.y-3*p2.y+p3.y)*ttt);
[smoothedPath addLineToPoint:pi];
}
// Now add p2
[smoothedPath addLineToPoint:p2];
}
// finish by adding the last point
[smoothedPath addLineToPoint:POINT(points.count - 1)];
return smoothedPath;
}

经过对比上边方式一与方式二的差异不大;

最后

以上就是可靠大门为你收集整理的iOS 画笔轨迹优化的全部内容,希望文章能够帮你解决iOS 画笔轨迹优化所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部