0

これらのメソッドを使用して aCGContextを myに保存できるようにしたい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();
         }
     }
}

ところで、知りたい場合は、undoDrawing という名前の NSUndoManager があります。

4

1 に答える 1

1

CGContext後で必要になったときに無効になるスタックの一部であるため、保存したくないと思います。代わりに、画像自体を保存してください。まず、フレーム全体にコンテキストを作成しないでください。currentPoint と lastPoint の間の長方形が必要です。次に、描画を開始する前に、現在のコンテキスト (UIGraphicsGetImageFromCurrentImageContext ()) から画像を取得し、それをフレームと共に NSUndoManager に保存します。

汚い四角形だけに描画を最適化することに慣れていない場合は、これのより単純なバージョンから始めて、描画する前に画像全体を undomanager に保存します。

于 2009-12-21T05:40:54.447 に答える