2012-04-19 108 views
0

enter image description here我正在使用以下代码在iphone中创建画笔。如何在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;  
} 

它的工作,但同时提请有一定的距离后,一些点在该行正在drawn.i要删除这些点,并希望顺利直line.How我可以做到这一点?

回答

0

您使用相同的图像一遍又一遍地乘以旧图像,对吧? 我会尝试在每次触摸时使用新鲜的上下文。然后在现有图像上乘以它。 也许这是奇怪的外观的原因?

如果开始和结束点不同,也可以添加检查。

而且你应该制作一个额外的绘图方法。在TouchesMoved和TouchesEnded中有冗余代码。

+0

即使我改变模式以正常的效果come.that不是bcoz一次again.it使用同样的图像也谈到第一次 – Bhoomi 2012-04-19 15:17:17

+0

告诉我们结果的截图..也许没有什么帮助。 – calimarkus 2012-04-19 15:52:18

+0

所以只有在使用透明颜色绘制时才会出现该问题?坚实的蓝色线似乎是好的?您可以保存touchesmoved的所有位置,并在每次更改时重新绘制整个当前行。 – calimarkus 2012-04-20 19:54:16