在iphone中绘制画笔时如何去除线条上的点?

在iphone中绘制画笔时如何去除线条上的点?,iphone,drawing,paint,cgcontextref,Iphone,Drawing,Paint,Cgcontextref,我使用以下代码在iphone中创建画笔 @interface Canvas : UIImageView { CGPoint location; } @property CGPoint location; .m file @synthesize location; - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject];

我使用以下代码在iphone中创建画笔

@interface Canvas : UIImageView {
    CGPoint location;
}

@property CGPoint location;
.m file
@synthesize location;

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];   
    self.location = [touch locationInView:self];    
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:self];  

    UIGraphicsBeginImageContext(self.frame.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext();  
    //CGContextSetBlendMode(ctx, kCGBlendModeOverlay);


    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetBlendMode(ctx, kCGBlendModeNormal);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    //CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, location.x, location.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    location = currentLocation;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:self];

    UIGraphicsBeginImageContext(self.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();  
  //  CGContextSetBlendMode(ctx, kCGBlendModeOverlay);

    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetBlendMode(ctx, kCGBlendModeNormal);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, location.x, location.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    location = currentLocation;     
}

这是可行的,但在绘制时,在绘制的直线上有一些点经过一定距离。我想删除这些点,并希望直线平滑。我如何才能做到这一点?

您正在反复使用同一图像,并将其与旧图像相乘,对吗? 我会尝试在每次触摸时使用新的上下文。然后将其乘以现有图像。 也许这就是这种奇怪表情的原因

如果起点和终点不同,也可以添加检查


你应该为画画做一个额外的方法。TouchesMoved和TouchesEnded中有多余的代码。

即使我将模式更改为正常,效果也会出现。这不是反复使用同一图像的原因。如果使用透明颜色绘制,问题只会出现在第一次出现时,它也会出现?蓝色实线看起来可以吗?您可以保存touchesmoved的所有位置,并在每次更改时新绘制整个当前行。