1

I am working with unod redo operations on CgLayer, I have tried some code, but not able to get it working, dont know , where I am getting wrong, below is my code, which i have written

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;
    }      
}

On touches ended , I am converting the layer into NSValue and storing as keyValue pair into NSDictionary and then adding the dictionary object to 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 Undo functionality

- (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];
}

I think its here where I am goin wrong. So friends please help me out.

From the comments below, I have added the function, where its draws into Cglayer(this function I am calling into 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);    
}

Regards Ranjit

4

2 回答 2

1

实现 Undo 和 Redo 的最佳方法是将其实现NSUndoManager为简要描述,您不必保存要撤消或重做的 Object 的每个状态,NSUndoManager 本身会为您完成...

实现这一目标的步骤是:

1- 初始化 NSUndoManager。

2- 使用指定的函数调用在 NSUndoManager 对象中注册对象状态,稍后将为您讨论这个问题。

3-使用 NSUndoManager 对象中的撤消或重做或清除操作功能。

来自我的工作解决方案

-在 .h 文件中

@property (nonatomic,retain) NSUndoManager *undoManager;
  • 在 .m 文件中

    @synthesize undoManager;

  • 在“viewDidLoad”方法中——初始化你的 NSUndoManager

    undoManager = [[NSUndoManager alloc] init];

假设您的类中有一个使用捏合放大/缩小的函数,因此在“viewDidLoad”中您将拥有

UIPinchGestureRecognizer *pinchGesture =
    [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    pinchGesture.delegate = (id)self;
    [self.view addGestureRecognizer:pinchGesture];

所以捏会放大/缩小这样注意“MyImageView”是我们想要放大/缩小的图像

- (void)pinch:(UIPinchGestureRecognizer*)recognizer{

    if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateChanged) {
        NSLog(@"gesture.scale = %f", recognizer.scale);

        CGFloat currentScale = self.MyImageView.frame.size.width / self.MyImageView.bounds.size.width;
        CGFloat newScale = currentScale * recognizer.scale;
        //Here is the line that register image to NSUndoManager before making and adjustments to the image "save current image before changing the transformation"
        //Add image function is function that fired when you Make undo action using NSUndoManager "and so we maintain only image transformation that changed when you zoom in/out"
         [[undoManager prepareWithInvocationTarget:self] AddImage:self.MyImageView.transform];

        if (newScale < 0.5) {
            newScale = 0.5;
        }
        if (newScale > 5) {
            newScale = 5;
        }

        CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
        self.MyImageView.transform = transform;
        recognizer.scale = 1;
    }
}

-AddImage 函数将当前图像转换状态保存在 NSUndoManager 中。

-(void)AddImage:(CGAffineTransform)sender{
    CGAffineTransform transform = sender;
    self.MyImageView.transform = transform;
}

因此,如果您有执行撤消操作的按钮

-(IBAction)OptionsBtn:(id)sender{
  if ([undoManager canUndo]) {
      [undoManager undo];
  }
}

因此,如果您想取消所有操作,则有两种方法

while ([undoManager canUndo]) {
   [undoManager undo];
}

或者

[undoManager removeAllActions];
于 2012-07-18T12:35:49.057 回答
0
- (void)redrawLine
{
    NSDictionary *lineInfo = [m_pathArray lastObject];
    NSValue *val = [lineInfo valueForKey:@"IMAGE"];
    CGContextRef context1 = (CGContextRef) [val pointerValue];
    CGContextDrawLayerAtPoint(context1, CGPointMake(00, 00),layerToShow);
    [self setNeedsDisplayInRect:self.bounds];
}

只需使用您的代码更新此方法

于 2012-08-23T05:57:33.520 回答