CoreGraphics:使用手指笔划擦除部分图像?

Jor*_*dan 3 iphone drawing image core-graphics objective-c

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

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

-(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();

}
Run Code Online (Sandbox Code Playgroud) iOS模拟器

Vla*_*ina 8

您不必使用CGContextClearRect清除笔划.

相反 CGContextSetBlendMode(context, kCGBlendModeClear)

此调用更改颜色混合模式,使绘制操作清除位图而不是使用颜色绘制.

然后,您可以绘制连接触摸位置的线条,以便没有间隙.

要切换回正常渲染吗 CGContextSetBlendMode(context, kCGBlendModeNormal)

使用不同的混合模式可能非常有用.