CoreGraphics: Using finger strokes to erase part of image?

巧了我就是萌 提交于 2019-12-21 20:37:58

问题


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

}


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5808244/coregraphics-using-finger-strokes-to-erase-part-of-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!