I work with unod redo operations on CgLayer, I tried some code, but could not get it to work, I donβt know where I am wrong, below is my code that I wrote
this is my drawRect function
- (void)drawRect:(CGRect)rect { m_backgroundImage = [UIImage imageNamed:@"bridge.jpg"]; CGPoint drawingTargetPoint = CGPointMake(0,0); [m_backgroundImage drawAtPoint:drawingTargetPoint]; switch(drawStep) { case DRAW: { CGContextRef context = UIGraphicsGetCurrentContext(); if(myLayerRef == nil) { myLayerRef = CGLayerCreateWithContext(context, self.bounds.size, NULL); } CGContextDrawLayerAtPoint(context, CGPointZero, myLayerRef); break; } case UNDO: { [curImage drawInRect:self.bounds]; break; } default: break; } }
In the end, I convert the layer to NSValue and save it as keyValue to NSDictionary, and then add the dictionary object to the array.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSValue *layerCopy = [NSValue valueWithPointer:myLayerRef]; NSDictionary *lineInfo = [NSDictionary dictionaryWithObjectsAndKeys:layerCopy, @"IMAGE", nil]; [m_pathArray addObject:lineInfo]; NSLog(@"%i",[m_pathArray count]); }
below is my Cancel function
- (void)undoButtonClicked { if([m_pathArray count]>0) { NSMutableArray *_line=[m_pathArray lastObject]; [m_bufferArray addObject:[_line copy]]; [m_pathArray removeLastObject]; drawStep = UNDO; [self redrawLine]; } }
I think here where I am mistaken. So friends, please help me.
From the comments below, I added a function where it draws in Cglayer (I call this function in touchhesMovedEvent.
- (void) drawingOperations { CGContextRef context1 = CGLayerGetContext(myLayerRef); CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2); CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1); CGContextMoveToPoint(context1, mid1.x, mid1.y); CGContextAddQuadCurveToPoint(context1, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y); CGContextSetLineCap(context1, kCGLineCapRound); CGContextSetLineWidth(context1, self.lineWidth); CGContextSetStrokeColorWithColor(context1, self.lineColor.CGColor); CGContextSetAllowsAntialiasing(context1, YES); CGContextSetInterpolationQuality(context1, kCGInterpolationHigh); CGContextSetAlpha(context1, self.lineAlpha); CGContextStrokePath(context1); }
Ranjit Relations