2009-12-21 4 views
0

Je veux être en mesure d'enregistrer un CGContext dans mes NSUndoManager en utilisant ces méthodesComment pourrais-je économiser CGContext dans un 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++; 

et

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

Par ailleurs, si vous voulez savoir J'ai mon NSUndoManager nommé undoDrawing

Répondre

1

Je ne crois pas que vous voulez enregistrer le CGContext, car il fait partie d'une pile qui ne sera pas valide lorsque vous en aurez besoin plus tard. Au lieu de cela, il suffit de sauvegarder l'image elle-même. Tout d'abord, ne créez pas le contexte sur l'ensemble du cadre. Vous avez juste besoin du rectangle entre currentPoint et lastPoint. Ensuite, avant de commencer à dessiner, récupérez l'image dans le contexte actuel (UIGraphicsGetImageFromCurrentImageContext()) et enregistrez-la avec le cadre dans NSUndoManager.

Si vous n'êtes pas habitué à optimiser votre dessin uniquement au rectangle sale, commencez par la version la plus simple et sauvegardez simplement l'image entière dans l'undomanager avant de dessiner dessus.

Questions connexes