ios - iPhone平滑素描绘制算法

标签 ios iphone drawing quartz-graphics

我正在 iPhone 上开发一个素描应用程序。 我让它工作但不像这里看到的那样漂亮 enter image description here

我正在寻找任何使绘图平滑的建议 基本上,我所做的是当用户将手指放在我调用的屏幕上时

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

然后我用

在一个数组中收集一个触摸
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

当用户从屏幕上离开手指时,我调用

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

然后我使用

绘制数组中的所有点
NSMutableArray *points = [collectedArray points];   

CGPoint firstPoint;
[[points objectAtIndex:0] getValue:&firstPoint];

CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);

for (int i=1; i < [points count]; i++) {
    NSValue *value = [points objectAtIndex:i];
    CGPoint point;
    [value getValue:&point];    
    CGContextAddLineToPoint(context, point.x, point.y);

} 

CGContextStrokePath(context);
UIGraphicsPushContext(context);

现在我想改进绘图,使其更像“速写本”App enter image description here

我认为与信号处理算法有关以重新排列数组中的所有点,但我不确定。任何帮助将非常感激。

提前致谢:)

最佳答案

CGPoint midPoint(CGPoint p1, CGPoint p2)
{

    return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];

    previousPoint1 = [touch previousLocationInView:self];
    previousPoint2 = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];

    previousPoint2 = previousPoint1;
    previousPoint1 = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];


    // calculate mid point
    CGPoint mid1 = midPoint(previousPoint1, previousPoint2); 
    CGPoint mid2 = midPoint(currentPoint, previousPoint1);

    UIGraphicsBeginImageContext(self.imageView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.imageView.image drawInRect:CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height)];

    CGContextMoveToPoint(context, mid1.x, mid1.y);
    // Use QuadCurve is the key
    CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y); 

    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, 2.0);
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextStrokePath(context);

    self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

}

关于ios - iPhone平滑素描绘制算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5076622/

相关文章:

ios - 按下完成后停止 AV 播放器

ios - 向 TextView 中的文本添加彩色项目符号

iphone - 如何确保应用程序退出时 NSUserDefaults 设置保存到磁盘?

ios - 如何使用 UIPageVIewController 以及如何更改 PageControl 的背景? - swift

ios - animateWithDuration(有时)跳到最终位置而不使用动画

ios - 是否有任何工作算法可以在所有 iOS 设备(有或没有 M7 芯片)上计算步数?

python - 用python画动态图

macos - 使用 Core Graphics 绘制雾

ios - iOS 上保存的图像上的 OpenGL ES 结果不正确(部分透明边缘为黑色)

ios - 克服 iPhone 5 系列和 iPhone 4 系列之间的 CPU 功率差异。 iPhone优化