CoreGraphics:使用手指笔划擦除部分图像? [英] CoreGraphics: Using finger strokes to erase part of image?

查看:139
本文介绍了CoreGraphics:使用手指笔划擦除部分图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制代码以擦除部分图像。我不是CoreGraphics的专家,可以使用一些帮助。

I'm working on drawing code to erase part of an image. I'm not an expert on CoreGraphics and could use some help.

这个例程工作正常,但是,当快速移动时,它会失去触摸(不是很平滑)。可以修改此例程以使CGContextClearRect更流畅吗?有更好,更快的方法吗?

This routine works fine, however, when moving fast, it loses touches (Not very smooth). Can this routine be modified to make CGContextClearRect smoother? Is there a better, faster way to do this?

-(void)drawRect:(CGRect)rect {

    if (!myDrawing) { // touchpoints stored here
        myDrawing = [[NSMutableArray alloc] initWithCapacity:0];
    }
    UIGraphicsBeginImageContext(frontImage.frame.size);
    [frontImage.image drawInRect:CGRectMake(0, 0, frontImage.frame.size.width, frontImage.frame.size.height)];

    if ([myDrawing count] > 0) {
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5);
        CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGImageAlphaNone );
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1, 0, 0, 10);

        for (int i = 0 ; i < [myDrawing count] ; i++) {
            NSArray *thisArray = [myDrawing objectAtIndex:i];

            if ([thisArray count] > 2) {
                float thisX = [[thisArray objectAtIndex:0] floatValue];
                float thisY = [[thisArray objectAtIndex:1] floatValue];
                CGContextBeginPath(UIGraphicsGetCurrentContext());

                for (int j = 2; j < [thisArray count] ; j+=2) {
                    thisX = [[thisArray objectAtIndex:j] floatValue];
                    thisY = [[thisArray objectAtIndex:j+1] floatValue];

                CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(thisX, thisY, 10, 10));
                }
            }
        }

    }
    frontImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

}




推荐答案

您不必使用 CGContextClearRect 来清除笔划。

You don't have to use CGContextClearRect to clear strokes.

而是 CGContextSetBlendMode(context,kCGBlendModeClear)

此次通话更改颜色混合模式,使绘图操作清除位图而不是用颜色绘图。

This call changes the color blending mode in such a way that drawing operations would be clearing bitmap instead of drawing with color.

然后你可以画出连接触摸位置的线条,这样就可以了没有差距。

Then you can just draw lines which connect touch locations so that there are no gaps.

要切换回正常渲染,请执行 CGContextSetBlendMode(context,kCGBlendModeNormal)

To switch back to normal rendering do CGContextSetBlendMode(context, kCGBlendModeNormal)

使用不同的混合模式非常有用。

Using different blending modes can be very helpful.

这篇关于CoreGraphics:使用手指笔划擦除部分图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆