简体   繁体   English

我如何将CGContext保存到NSUndoManager中

[英]How would I save a CGContext into an NSUndoManager

I want to be able to save a CGContext into my NSUndoManager using these methods 我希望能够使用以下方法将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++;

and

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

By the way if you want to know I have my NSUndoManager named undoDrawing 顺便说一句,如果您想知道我的NSUndoManager名为undoDrawing

I don't believe you want to save the CGContext , since it's part of a stack that will not be valid when you need it later. 我不相信您要保存CGContext ,因为它是堆栈的一部分,以后需要时将无效。 Instead, just save the image itself. 而是只保存图像本身。 First, don't create the context over the whole frame. 首先,不要在整个框架上创建上下文。 You just need the rectangle between currentPoint and lastPoint. 您只需要currentPoint和lastPoint之间的矩形。 Then, before you start drawing, grab the image from the current context (UIGraphicsGetImageFromCurrentImageContext ()) and save that off along with the frame to the NSUndoManager. 然后,在开始绘制之前,请从当前上下文(UIGraphicsGetImageFromCurrentImageContext())获取图像,并将其与框架一起保存到NSUndoManager。

If you're unfamiliar with optimizing your drawing just to the dirty rectangle, start with the simpler version of this and just save the entire image to the undomanager before drawing on it. 如果您不熟悉仅对脏矩形进行优化的绘图,请从简单版本开始,然后将整个图像保存在undomanager上,然后再进行绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM