CoreGraphics: Using finger strokes to erase part o

2019-04-10 19:37发布

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

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

}
iOS Simulator

2条回答
放荡不羁爱自由
2楼-- · 2019-04-10 20:19

You should never do anything but drawing in your drawRect code. That really slows down the process. Instead, think of perhaps splitting off rendering to a separate thread. That will really speed things up.

查看更多
我想做一个坏孩纸
3楼-- · 2019-04-10 20:23

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

Instead do 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.

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

Using different blending modes can be very helpful.

查看更多
登录 后发表回答