0

この種の投稿が何度も尋ねられたことは知っていますが、解決策が見つからないため、Google やこのサイトでも多くの検索を行ったので、この質問を投稿しています。UIBezierPath などを使用せずに、描画した CGContext 行を元に戻したいと思います。方法はありますか?描画に使用するコードは次のとおりです。

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:mainImageView];

UIGraphicsBeginImageContext(mainImageView.frame.size);
[mainImageView.image drawInRect:CGRectMake(0, 0, mainImageView.frame.size.width, mainImageView.frame.size.height)];

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), dimension);

const CGFloat *components = CGColorGetComponents([color CGColor]);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], components[3]);

CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());

mainImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

lastPoint = currentPoint;}

ありがとう。

4

2 に答える 2

1

元に戻すを追加するには:

一度ペイントすると、画像内の情報を表示することはできません...

変更するたびに画像のコピーを保持する必要があります(メモリの量は多くなりますが、変更された領域のコピーだけを保持することで削減できます)。または、ユーザーの描画手順を記録してから再生する必要があります。元に戻した後。

于 2013-02-07T13:59:10.917 に答える
0

画像に描画しているので、画像を無効にするだけで、目に見える線がなくなります。

mainImageView.image = NULL;
// Or
mainImageView.image = originalImage; // Where originalImage is your background if you're using one.

アップデート

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:mainImageView];

    UIGraphicsBeginImageContext(mainImageView.frame.size);

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), dimension);

    const CGFloat *components = CGColorGetComponents([color CGColor]);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], components[3]);

    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());

    // Add the line as a subView.
    [mainImageView addSubView:[[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()]];
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

}

- (void)undoLastLine
{
    [mainImageView.subviews.lastObject removeFromSuperview];
}
于 2013-02-07T13:57:44.260 に答える