【iOS】使用一组点画出平滑的曲线

今天在项目中需要人脸上的点来勾勒出人脸的轮廓,我的想法是将要画的点存入一个数组,使用了UIBezierPath来连接每一个点。但是这样画出来的图是折线,显得过于生硬。查了若干资料,后来终于在stackoverflow上找到了一个很好的解决办法

这个做法的原理其实是在每两个点之间加入一些点,来使得两个点可以平滑的过度。这其实就是Centripetal Catmull–Rom spline的思想。

于是便照葫芦画瓢,写出了我的代码

#define POINT(_INDEX_) [(NSValue *)[points objectAtIndex:_INDEX_] CGPointValue]
- (void)smoothedPathWithPoints:(NSArray *) pointsArray andGranularity:(NSInteger)granularity {
    
    NSMutableArray *points = [pointsArray mutableCopy];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetAllowsAntialiasing(context, YES);
    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
    CGContextSetLineWidth(context, 0.6);
    
    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)];
    
    CGContextAddPath(context, smoothedPath.CGPath);
    CGContextDrawPath(context, kCGPathStroke);
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在iOS开发过程中,我们会经常遇到画线的功能,比如线性图。 目前iOS画线有两大类方法 (我所知道的)。1、基于C...
    平原河流阅读 11,574评论 14 20
  • 前言 本文只要描述了iOS中的Core Animation(核心动画:隐式动画、显示动画)、贝塞尔曲线、UIVie...
    GitHubPorter阅读 9,017评论 7 11
  • 如果有个异性经常找你聊天,然后对你说“我只想和你更多地交流思想”。十有八九你会觉得他或者她对自己有意思,人之常情嘛...
    MitoNatsume阅读 1,324评论 0 0
  • 夏至金乌复回首, 丝绦梅雨何时休。 月夜把盏伤四序, 仰看七曜梦九州。
    LinkinPark_9100阅读 1,594评论 0 0
  • 我们本是一个独立的个体,如果不是因为相爱,就不会也不必在一起。 我很欣赏杨澜,她是一位相当优雅的女性。 这本书是她...
    微光222阅读 3,940评论 8 3

友情链接更多精彩内容