12

指をスワイプして前面のUIImageのコンテンツを消去し、UIViewの背面であるUIImageを表示するにはどうすればよいですか。

次のコードを使用して、- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 前面の画像を消去しました。

-(void)eraseDrawingContents
{
    UIGraphicsBeginImageContext(frontImage.frame.size);
    [frontImage.image drawInRect:CGRectMake(0, 0, frontImage.frame.size.width, frontImage.frame.size.height)];

    CGContextSaveGState(UIGraphicsGetCurrentContext());
    CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25.0);
    CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(0, 0), 50, [[UIColor whiteColor]CGColor]);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, lastPoint.x, lastPoint.y);
    CGPathAddLineToPoint(path, nil, currentPoint.x, currentPoint.y);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
    CGContextAddPath(UIGraphicsGetCurrentContext(), path);
    CGContextStrokePath(UIGraphicsGetCurrentContext());

    frontImage.image = UIGraphicsGetImageFromCurrentImageContext();
    CGContextRestoreGState(UIGraphicsGetCurrentContext());
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;
}

次の出力を達成しました。

ここに画像の説明を入力

しかし、私はこのような出力が必要です。

ここに画像の説明を入力

どうすればこの機能を実現できますか?

私を助けてください。

4

3 に答える 3

6

これを試して

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


    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];


   self.imgView.image = [self.imgView.image eraseImageAtPoint:location inImageView:self.imgView  fromEraser:eraserImg];



}



- (UIImage *)eraseImageAtPoint: (CGPoint)point inImageView: (UIImageView *)imgView fromEraser: (UIImage *)eraser
{
    UIGraphicsBeginImageContext(imgView.frame.size);

    [self drawInRect:CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height)];

    [eraser drawAtPoint:point blendMode:kCGBlendModeDestinationOut alpha:1.0];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;

}

そして、下の画像を消しゴムとして使用すると、これは完全に機能します。

消しゴム画像

このコードは私の最後で機能しており、次の結果が得られます。画像1

画像2

画像3

画像4

于 2013-09-13T13:15:34.147 に答える
2

このサンプルコードを確認してください

このような機能を提供します

ここに画像の説明を入力

ここに画像の説明を入力

于 2013-09-13T11:37:37.717 に答える