How to delete a drawn CGPoint on a UIImageView

206 views Asked by At

I have a UIImageView that i draw on it points . I store my CGpoint in an NSArray with a capacity of 100. Every time i receive a new CGpoint i want to erase the last object from the array , remove it from the array and then draw the new CGpoint and add it to the array.

So how do i erase a CGpoint? and have only 100 CGpoint on my UIImageView?

My code:

- (void) drawLine:(CGPoint)pt onCanvas:(UIImageView *)canvas color:(UIColor *)color
{

UIGraphicsBeginImageContext(canvas.frame.size);
[canvas.image drawInRect:CGRectMake(0, 0, canvas.frame.size.width, canvas.frame.size.height)];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineCap(ctx, kCGLineCapButt);
CGContextSetLineWidth(ctx,10.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextAddEllipseInRect (ctx,CGRectMake(pt.x-2.0, pt.y-2.0, 4, 4));
CGContextSetFillColorWithColor(ctx,color.CGColor);
CGContextFillPath(ctx);

CGContextStrokePath(ctx);
canvas.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
1

There are 1 answers

0
Sulthan On BEST ANSWER

You cannot remove points from an image. It's an image - imagine it like a photo you have taken and then you try to remove the background from it. It's impossible.

The best thing you can do is not to use a double buffer - just keep the points in your array and paint them all when needed. Then you won't have to remove anything. It's true this could affect the performance a bit but 100 points should still be fine.