2013-04-06 46 views
0

我想在uiimageview中刪除touch事件中繪製的線條。 我想在touchesEnded中調用setNeedsDisplay,但它不起作用。如何擦除在uiimageview上繪製的線條?

在Canvas.m我:

- (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(); 
    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
    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; 
} 

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

    [self setNeedsDisplay]; 
} 

回答

1

簡單的解決方案 - 保存原始圖像的開頭,當你要刪除的行只是做self.image = savedImage

然而,更好的解決方案(以更好的表現)將不會改變圖像,而是具有對UIImageView的透明視圖並在其內部繪製。該圖也可以更簡單(例如,具有CGImageRef緩衝區,而不是每次由UIGraphicsBeginImageContext創建圖像上下文)。