Pregunta

Quiero poder guardar un CGContext en mi NSUndoManager Usando estos métodos

- (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++;

y

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

Por cierto, si quieres saber, tengo mi nsundomanerer llamado sindotar

¿Fue útil?

Solución

No creo que quieras salvar el CGContext, dado que es parte de una pila que no será válida cuando la necesite más tarde. En cambio, simplemente guarde la imagen en sí. Primero, no cree el contexto en todo el marco. Solo necesita el rectángulo entre CurrentPoint y LastPoint. Luego, antes de comenzar a dibujar, tome la imagen del contexto actual (UigraphicSgetImageFromCurrentImageContext ()) y guárdela junto con el marco al Nsundomanerer.

Si no está familiarizado con la optimización de su dibujo solo al rectángulo sucio, comience con la versión más simple de esto y simplemente guarde la imagen completa al indemandante antes de dibujarlo.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top