0

我希望能够使用这些方法将 a 保存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

4

1 回答 1

1

我不相信你想保存CGContext,因为它是堆栈的一部分,当你以后需要它时它是无效的。相反,只需保存图像本身。首先,不要在整个框架上创建上下文。您只需要 currentPoint 和 lastPoint 之间的矩形。然后,在开始绘制之前,从当前上下文中获取图像(UIGraphicsGetImageFromCurrentImageContext ())并将其与帧一起保存到 NSUndoManager。

如果您不熟悉仅针对脏矩形优化绘图,请从更简单的版本开始,然后在绘制之前将整个图像保存到 undomanager。

于 2009-12-21T05:40:54.447 回答