使用UIBezierPath擦除CGContextRef的羽毛

时间:2017-10-22 20:09:52

标签: objective-c core-graphics uibezierpath

我正在使用UIBezierPath和CGBlendMode:kCGBlendModeClear从CGContextRef中删除绘图。我正在删除线宽为20的笔划路径。它使路径变得严酷。有没有办法实现从CGContextRef中擦除绘图的羽毛笔划。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = [[touches allObjects] firstObject];
    [drawingPath moveToPoint:[self getConvertedPoint:touch]];
    currentPathData.drawingPath.lineJoinStyle =  kCGLineJoinMiter;  
      CGContextSetLineWidth(_imageContext,  currentPathData.line_width);    
}

-(CGPoint)getConvertedPoint:(UITouch *)touch{
    CGSize size = CGSizeMake(self.image.size.width * self.image.scale, self.image.size.height * self.image.scale);
    CGPoint touchPoint =  [touch locationInView:self];
    touchPoint = fromUItoQuartz(touchPoint, self.bounds.size);
    touchPoint = scalePoint(touchPoint, self.bounds.size, size);
    return touchPoint;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
    if(_shouldDrawImage){
        if(!self.image){
            return ;
        }
        UITouch *touch = [[touches allObjects] firstObject];
        const CGPoint line_point = [self getConvertedPoint:touch];
        [drawingPath  addLineToPoint:line_point];
        UIImage *image = [self drawStrokeImage:line_point];
        [super setImage:image];
    }
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    _shouldDrawImage = NO;
    brushImageView.hidden = YES;
}

-(UIImage *)drawStrokeImage:(const CGPoint)line_point{
    CGContextSetBlendMode(_imageContext, blendMode);
    CGContextAddPath(_imageContext, [drawingPath CGPath]);
    CGContextStrokePath(_imageContext);    
    CGImageRef cgImage = CGBitmapContextCreateImage(_imageContext);
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    return image;
}

我从上面得到了这个结果,我无法弄清楚如何在笔画路径中应用羽毛:

I get this result from the above, I can not figure out how to apply feather in stroke path

0 个答案:

没有答案