以下是我使用 CGContext 进行自由手绘的代码。我想使用 alpha 值为 0.6 的颜色和混合模式为kCGBlendModeColor
. 但是在绘图时,我得到以下效果:颜色重叠并变暗。我想要不重叠和平滑的绘制。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
lastTouch = [touch locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentTouch = [touch locationInView:self];
CGFloat brushSize = 35;
UIColor *color = [UIColor blueColor];
CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;
if ([color respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
[color getRed:&red green:&green blue:&blue alpha:&alpha];
} else {
const CGFloat *components = CGColorGetComponents(color.CGColor);
red = components[0];
green = components[1];
blue = components[2];
alpha = components[3];
}
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, brushSize);
CGContextSetRGBStrokeColor(context, red, green, blue, 0.6) ;
CGContextSetBlendMode(context, kCGBlendModeColor);
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
CGContextStrokePath(context);
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastTouch = [touch locationInView:self];
}