last content of CGcontext is not saved?
I am using following code to draw a line based on touch moved as it is in MS Paint? It works fine.but when I try to draw a line again after touch Ended, The previous line is erased..any help please?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2)
{
//drawImage.image = nil;
//alphavalue 开发者_运维百科= 0.0;
//isErase = TRUE;
//return;
}
lastPoint = [touch locationInView:self.view];
}
UIGraphicsBeginImageContext
will create a new image context every time you enter the touchesMoved
routine. You will need to find a way to save off the line's start and end points so it can be rendered to a more permanent context elsewhere, or come up with some other solution that preserves the context.
a) don't hold onto the graphics context like that
b) you want to draw the image from within your override of drawRect:
. alternatively, you can hold onto the data you need and invalidate the rect you need to draw in touchesMoved. then use the state you've stashed to draw from within drawRect:
.
the bottom line is you should invalidate, then draw when requested. drawing beyond that context will leave you with unpredictable drawing results (cropped rects, unexpected ordering and other behavior), as well as forcing the program to draw multiple times.
Try first creating a CGContext using CGBitmapContextCreate(), save it, and then use that same context for all your drawing.
精彩评论