在iOS中绘图时计算控制点

时间:2014-01-02 11:16:46

标签: ios core-graphics cgcontext uibezierpath cgpath

我正在制作一个绘图应用程序,用户可以用手指或手写笔绘图/书写。为此,我在我的应用程序中引用了https://github.com/yusenhan/Smooth-Line-View中的代码。

我所面临的问题是,有时写作非常接近的写作并不是很顺利。

所以我想,我在获得控制点时犯了一些错误。

以下是我的代码

//找到中点

CGPoint midPoint(CGPoint p1, CGPoint p2)
{
    return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);
}

#pragma mark Gesture handle
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];
    //LocationInView returns the current location of the reciever in coordinate system of the given View.
    m_previousPoint1 = [touch locationInView:self];
    m_previousPoint2 = [touch locationInView:self];
    m_currentPoint   = [touch locationInView:self];    

    [self touchesMoved:touches withEvent:event];}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{          
    //AnyObject:- Returns one of the objects in the set, or nil if the set contains no objects.
    UITouch *touch  = [touches anyObject];

    m_previousPoint2  = m_previousPoint1;
    m_previousPoint1  = m_currentPoint;
    m_currentPoint    = [touch locationInView:self];

    if(m_drawStep != ERASE)
    {
        m_drawStep = DRAW;
        m_drawing  = TRUE;        
    }    
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);//creates a graphics context suitable for use as an image(size of the image,opquae,scale, if scale = 0.0, means platform will take care of scaling)
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    m_curImage = UIGraphicsGetImageFromCurrentImageContext();// to turn the context into a UIImage    
    UIGraphicsEndImageContext();

 }

- (void)drawRect:(CGRect)rect
{
   CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2);
   CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);            

   [m_curImage drawAtPoint:CGPointMake(0, 0)];

   CGContextRef context = UIGraphicsGetCurrentContext();//Get a reference to current context(The context to draw)
   [self.layer renderInContext:context];
   //Simply keep referring to this context in below functions with proper arguments.
   CGContextMoveToPoint(context, mid1.x, mid1.y);//Position the current point
   CGContextAddQuadCurveToPoint(context, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y);     
   CGContextSetLineCap(context, kCGLineCapRound);
   CGContextSetBlendMode(context, kCGBlendModeNormal);
   CGContextSetLineJoin(context, kCGLineJoinRound);
   CGContextSetLineWidth(context, self.lineWidth);
   CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
   CGContextSetShouldAntialias(context, YES);
   CGContextSetAllowsAntialiasing(context, YES);
   CGContextSetAlpha(context, self.lineAlpha);           
   CGContextSetFlatness(context, 0.6);
   CGContextStrokePath(context);//paints(fills) the line along the current path.            
}

根据我的说法,我在CGContextAddQuadCurveToPoint传递的分数可以调整,但如何调整,我没有得到。我阅读了文档以找到Bezier Curve的控制点,但我不理解。

所以朋友们,请帮助我理解我哪里出错了。

1 个答案:

答案 0 :(得分:3)

你遇到的问题是,当用户慢慢地拖动手指时,你得到的控制点相距太远,所以你会得到较大的线段,而不是小的像素变化会给你一个平滑的曲线

从现有曲线计算Bezier控制点非常困难,因为控制点不在线上。

最好使用Catmull-Rom曲线。 Erica Sadun的 OUTSTANDING 书籍“高级iOS 6开发者手册”中有一个很好的“食谱”,其中包括基于Catmull-Rom样条曲线平滑的工作代码。 Catmull-Rom样条曲线使用曲线上的控制点。

我强烈推荐购买那本书。食谱4.3的示例代码将完全符合您的要求。