0

I am working on an iPhone application of drawing, user can draw a line and can remove as well so can any body help me how to remove the line or drawing?

code for draw

- (void)drawRect:(CGRect)rect {

    float newHeight;
    float newWidth;

    if (!myDrawing) {
        myDrawing = [[NSMutableArray alloc] initWithCapacity:0];
    }
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    if (myPic != NULL) {
        float ratio = myPic.size.height/460;
        if (myPic.size.width/320 > ratio) {
            ratio = myPic.size.width/320;
        }

        newHeight = myPic.size.height/ratio;
        newWidth = myPic.size.width/ratio;

        [myPic drawInRect:CGRectMake(0,0,newWidth,newHeight)];
    }
    if ([myDrawing count] > 0) {
        CGContextSetLineWidth(ctx, 5);

        for (int i = 0 ; i < [myDrawing count] ; i++) {
            NSArray *thisArray = [myDrawing objectAtIndex:i];

            if ([thisArray count] > 2) {
                float thisX = [[thisArray objectAtIndex:0] floatValue];
                float thisY = [[thisArray objectAtIndex:1] floatValue];

                CGContextBeginPath(ctx);
                CGContextMoveToPoint(ctx, thisX, thisY);

                for (int j = 2; j < [thisArray count] ; j+=2) {
                    thisX = [[thisArray objectAtIndex:j] floatValue];
                    thisY = [[thisArray objectAtIndex:j+1] floatValue];

                    CGContextAddLineToPoint(ctx, thisX,thisY);
                }
                CGContextStrokePath(ctx);
            }
        }
    }
}

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

    [myDrawing addObject:[[NSMutableArray alloc] initWithCapacity:4]];

    CGPoint curPoint = [[touches anyObject] locationInView:self];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
}

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

    CGPoint curPoint = [[touches anyObject] locationInView:self];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];

    [self setNeedsDisplay];
}

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

    CGPoint curPoint = [[touches anyObject] locationInView:self];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];

    [self setNeedsDisplay];

}

-(void)cancelDrawing {

    [myDrawing removeAllObjects];
    [self setNeedsDisplay];

}
Anil Kothari
  • 7,653
  • 4
  • 20
  • 25
Mashhadi
  • 3,004
  • 3
  • 46
  • 80
  • Please post your code how do you draw. without which we can't help.. – Janak Nirmal Jan 25 '12 at 06:30
  • When anybody devote the question then please also write the reason here of devoting. – Mashhadi Jan 25 '12 at 06:35
  • My answer here should help you: [How can I erase UIBezierPath lines drawn on a transparent view above an image?](http://stackoverflow.com/questions/7979937/how-can-i-erase-uibezierpath-lines-drawn-on-a-transparent-view-above-an-image) – jrturton Jan 25 '12 at 08:07

1 Answers1

3

You can use CGContextSetStrokeColorWithColor and set color of background of your view while drawing line so it will look like you are erasing line.

And if you want to provide user undo facility while drawing than you can create following function for that,

-(void)undoDrawing {

    [myDrawing removeLastObject];
    [self setNeedsDisplay];

}

Hope this helps...

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
  • Thanks @Jennis but i have already undo functionality but infect problem is that user can place an image on the background, my view is not of only one color, if your add his Photo on background then this approach wil not work. – Mashhadi Jan 25 '12 at 07:44
  • May be you can set StrokeColor to clear color and draw ? I am not sure but just a guess.. – Janak Nirmal Jan 25 '12 at 09:20
  • Wow Good idea I'll Try this :) thatnx – Mashhadi Jan 25 '12 at 10:08