2009-12-21 69 views
0

我希望能夠保存CGContext到我NSUndoManager使用這些方法我將如何保存CGContext上到NSUndoManager

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 
      currentPoint.x = currentPoint.x; 
      currentPoint.y = currentPoint.y; 
      mouseSwiped = YES; 
      UIGraphicsBeginImageContext(self.view.frame.size); 
      [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
      CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES); 
      CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSizeVal); 
      CGContextSetAlpha(UIGraphicsGetCurrentContext(), drawingColorAlpha); 
      CGContextSaveGState(UIGraphicsGetCurrentContext()); //Probably right here 
      CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 
             drawingColorRed, 
             drawingColorGreen, 
             drawingColorBlue, 
             drawingColorAlpha); 
      CGContextBeginPath(UIGraphicsGetCurrentContext()); 
      CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 
            lastPoint.x, 
            lastPoint.y); 
      CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 
            currentPoint.x, 
            currentPoint.y); 
      CGContextStrokePath(UIGraphicsGetCurrentContext()); 
      drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); //drawImage is the UIImageView that I am drawing my context to 
       NSLog(@"current point x: %d current point y: %d",currentPoint.x, currentPoint.y); //Used for my purposes 
      lastPoint = currentPoint; 
      mouseMoved++; 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if (!optionsDisplayerVisible && canDraw) 
    { 
     if(!mouseSwiped) 
     { 
      UIGraphicsBeginImageContext(self.view.frame.size); 
      [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
      CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES); 
      CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSizeVal); 
      CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 
             drawingColorRed, 
             drawingColorGreen, 
             drawingColorBlue, 
             drawingColorAlpha); 
      CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
      CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
      CGContextStrokePath(UIGraphicsGetCurrentContext()); 
      CGContextFlush(UIGraphicsGetCurrentContext()); 
      drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); //drawImage is the UIImageView that I am drawing to 
      UIGraphicsEndImageContext(); 
     } 
    } 
} 

順便說一句,如果你想知道我有我的NSUndoManager命名爲undoDrawing

回答

1

我不相信你想要保存CGContext,因爲它是一個堆棧的一部分,當你以後需要時它不會有效。相反,只保存圖像本身。首先,不要在整個框架上創建上下文。你只需要currentPoint和lastPoint之間的矩形。然後,在開始繪製之前,從當前上下文(UIGraphicsGetImageFromCurrentImageContext())中獲取圖像,並將其與幀一起保存到NSUndoManager。

如果你不熟悉優化你的繪圖只是爲了骯髒的矩形,從這個簡單的版本開始,只需在繪製之前將整個圖像保存到undomanager。