deshacer problemas de rehacer con CGLayer
Estoy trabajando con Unod Redo Operations en CgLayer. He probado un código, pero no pude hacerlo funcionar, no sé, dónde me estoy equivocando, a continuación se encuentra mi código, que he escrito.
esta es mi funcion drawRect
- (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;
}
}
Al finalizar los toques, estoy convirtiendo la capa en NSValue y almacenándola como par keyValue en NSDictionary y luego agregando el objeto del diccionario a la matriz.
- (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]);
}
a continuación es mi funcionalidad Deshacer
- (void)undoButtonClicked
{
if([m_pathArray count]>0)
{
NSMutableArray *_line=[m_pathArray lastObject];
[m_bufferArray addObject:[_line copy]];
[m_pathArray removeLastObject];
drawStep = UNDO;
[self redrawLine];
}
}
//Redraw functions
- (void)redrawLine
{
NSDictionary *lineInfo = [m_pathArray lastObject];
NSValue *val = [lineInfo valueForKey:@"IMAGE"];
CGLayerRef layerToShow = (CGLayerRef) [val pointerValue];
CGContextRef context1 = CGLayerGetContext(layerToShow);
CGContextDrawLayerAtPoint(context1, CGPointMake(00, 00),layerToShow);
[self setNeedsDisplayInRect:self.bounds];
}
Creo que es aquí donde me equivoco. Así que amigos por favor ayudenme.
De los comentarios a continuación, he agregado la función, donde se dibuja en Cglayer (esta función estoy llamando a touchesMovedEvent.
- (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);
}
Saludos Ranjit