Drawing a picture in iOS

I am working on a graphical application, I have UIBezeirPath with which I draw in touchhesMoved and convert it to CGPath, and then draw CGlayer,

Here is my code

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { self.currentPath = [[DrawingPath alloc] init]; if(m_eraseButtonClicked) { [self.currentPath setPathColor:[UIColor backgroundColor]]; } else { [self.currentPath setPathColor:self.lineColor]; } CGPathRef cgPath = self.currentPath.path.CGPath; mutablePath = CGPathCreateMutableCopy(cgPath); } - (void)Erase { CGContextRef layerContext = CGLayerGetContext(self.DrawingLayer); CGContextSetBlendMode(layerContext,kCGBlendModeClear); CGContextSetLineWidth(layerContext, self.eraseWidth); CGContextBeginPath(layerContext); CGContextAddPath(layerContext, mutablePath); CGContextStrokePath(layerContext); } - (void)UndoRedoFunction { //Undo/Redo for(int i =0; i<[undoArray count];i++) { DrawingPath *drawPath = [undoArray objectAtIndex:i]; CGPathRef path = drawPath.path.CGPath; mutablePath = CGPathCreateMutableCopy(path); CGContextBeginPath(layerContext); CGContextAddPath(layerContext, mutablePath); CGContextSetLineWidth(layerContext, drawPath.pathWidth.floatValue); CGContextSetStrokeColorWithColor(layerContext, drawPath.pathColor.CGColor); CGContextSetFillColorWithColor(layerContext, drawPath.pathColor.CGColor); CGContextSetBlendMode(layerContext,kCGBlendModeNormal); CGContextStrokePath(layerContext); CGPathRelease(mutablePath); } } 

Erase works fine, because I use blendMode Clear, but when undoing / Redo, as you can see, I get pathColor from the path and stroke it with blenModeNormal, I see a white line,

Below image, after I write-> erase-> undo-> repeat

enter image description here

0
ios uikit core-graphics uibezierpath cgcontext
Feb 26 '14 at 6:11
source share
1 answer

Have you entered your code correctly? As shown, the -touchesMoved: withEvent method closes around line 14 immediately before the //Erase statement. This means that your CGContextRef is now a class variable, and your for loop is never executed by anyone, and I don’t think it will compile, so I wonder if some code is omitted.

0
Feb 26 '14 at 6:46
source share



All Articles