【问题标题】:How to get rid of this "points" between my lines when I am drawing?绘画时如何摆脱线条之间的“点”?
【发布时间】:2011-06-07 08:20:01
【问题描述】:

有没有一种简单的方法可以不在我的线条中画出这些点?
我不知道为什么会有这些点,因为在画线的过程中我从来没有从屏幕上松开手指。

我从一个绘图示例中得到代码。

// draw a line
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

mouseSwiped = YES;
UITouch *touch = [touches anyObject];   
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 0; // 20 only for 'kCGLineCapRound'
UIGraphicsBeginImageContext(self.view.frame.size);
//Albert Renshaw - Apps4Life
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSize); // for size
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, alpha); //values for R, G, B, and Alpha
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

lastPoint = currentPoint;

mouseMoved++;

if (mouseMoved == 10) {
    mouseMoved = 0;
}

    }

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


//Draw a dot
if(!mouseSwiped) {

    UIGraphicsBeginImageContext(self.view.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSize);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, alpha);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    CGContextFlush(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}
    }

这是最终版本,每行都有唯一的 alpha、颜色、brushSize:

- (void) updateDrawingBoard 
{
UIGraphicsBeginImageContext(self.drawImage.bounds.size);

for ( NSDictionary *dict in paths ) {

   UIBezierPath *p = (UIBezierPath*)[dict objectForKey:@"path"];
   p.lineWidth = [[dict objectForKey:@"size"]floatValue];
   [[UIColor colorWithRed:[[dict objectForKey:@"red"]floatValue] 
                     green:[[dict objectForKey:@"green"]floatValue] 
                      blue:[[dict objectForKey:@"blue"]floatValue] 
                     alpha:[[dict objectForKey:@"alpha"]floatValue]] setStroke];
  [p stroke];
}
[[UIColor colorWithRed:r green:g blue:b alpha:alpha] setStroke];
[path stroke];

self.drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint touchPoint = [[touches anyObject] locationInView:self.drawImage];

path = [[UIBezierPath bezierPath] retain];
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinBevel;
path.lineWidth = brushSize;
[path moveToPoint:touchPoint];

[self updateDrawingBoard];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint touchPoint = [[touches anyObject] locationInView:self.drawImage];

[path addLineToPoint:touchPoint];

NSDictionary   *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                        path,@"path",
                        [NSNumber numberWithFloat:r], @"red", 
                        [NSNumber numberWithFloat:g], @"green", 
                        [NSNumber numberWithFloat:b], @"blue", 
                        [NSNumber numberWithFloat:alpha], @"alpha", 
                        [NSNumber numberWithFloat:brushSize], @"size", nil];
[paths addObject:dict];
[path release];
path = nil;

[self updateDrawingBoard];
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint touchPoint = [[touches anyObject] locationInView:self.drawImage];

[path addLineToPoint:touchPoint];

[self updateDrawingBoard];
}

【问题讨论】:

  • alpha 来自哪里?看起来您可以通过将 alpha 设置为 1. 来解决此问题
  • 是的,我知道,alpha = 1 它可以工作。但我希望有透明的颜色来突出它下面的文本。
  • 你得到答案了吗?我也面临同样的问题。

标签: iphone xcode cgcontext uitouch


【解决方案1】:

问题是您正在通过绘制图像逐渐积累线条。在旧线和新线重叠的地方,您会看到过度绘制的“点”。

解决方案是在路径中累积点,并在每次使用该路径时重新绘制图像。由于您将绘制一条路径,而不是多条重叠路径,因此您不应该看到这些点。

代码大纲:

  • 在绘图开始前的某个时间,创建一个CGMutablePathRef
  • 当您有一个要添加到行中的新点时,请使用 CGPathAddLineToPoint
  • 绘制路径时,使用CGContextAddPath 将线添加到上下文中,然后根据需要进行填充或描边。你也可以使用CGContextDrawPath

或者,您可以使用UIBezierPath 而不是CGMutablePathRef,在这种情况下,步骤如下:

  • 创建一个UIBezierPath
  • 使用-addLineToPoint: 在路径中添加行。
  • 使用-stroke-fill 和类似方法将路径绘制到上下文中。

如果您不习惯直接使用 CoreGraphics,这可能会更简单。

我会删除中间图像并将此代码移动到视图类(LineDrawViewCanvasView 或其他东西),而不是将代码留在视图控制器中。视图可以直接将自己绘制到屏幕上,而不是更新图像。该视图将具有清除路径、撤消笔划和创建路径图像的方法。然后,视图控制器将使用这些来清除画布、撤消线条并保存绘图。您可以稍后通过配置线条样式的功能来丰富这一点。

【讨论】:

  • 好的,我现在可以画画了。但是如何保存视图或图像的路径?因为我在 UIImageView 中加载并将它设置为这个视图的子视图,所以我可以在它上面绘制。最后,我想将加载的图像与文件中的新路径一起保存。编辑了我的问题中的代码。
  • 使用UIGraphicsBeginImageContextWithOptions,就像您在旧代码中所做的那样。使图像的大小成为视图的边界大小,[self bounds].size。然后,像往常一样画画;你可以调用[self drawRect:[self bounds]],前提是你的边界原点仍然是(0, 0)。
  • 这应该如何工作?现在我的应用程序一直崩溃。我不知道把 UIGraphicsBeginImageContext 和 drawRect 函数放在哪里。编辑了我的代码。
  • 在这一点上,你正在挥舞。您需要查看Drawing and Printing Guide for iOS。如果您还有其他问题,请随时在 Stack Overflow 上提出新问题。
【解决方案2】:

几天来,我一直在尝试解决这个问题,尝试 Jeremy W. Sherman 的回答,这可能是一个非常好的主意,但对我的实施来说是不可行的。

在我的绘图应用程序中,我无法通过不断渲染路径来使 alpha 曲线相互融合,尽管点消失了。如果您只想要一条干净的 alpha 色路径,这可能是您要走的路。

但我找到了一个更简单的解决方案;这些调整通过混合实时创建了完美的 alpha 色路径(将其视为毡尖笔的风格):

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapButt);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeMultiply);

希望这可以帮助任何在 iOS 中进行简单 CG 绘画的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2018-10-17
    相关资源
    最近更新 更多