为不同的贝塞尔曲线路径填充单独的颜色

时间:2016-10-21 14:37:18

标签: ios core-graphics uibezierpath

我正在尝试为不同的bezierPaths添加单独的颜色,例如

UIGraphicsBeginImageContext(_v_sticker.frame.size);

UIColor *fillColor = [UIColor redColor];
[fillColor setFill];
UIColor *strokeColor = [UIColor blueColor];
[strokeColor setStroke];

p = [UIBezierPath bezierPath];
[p moveToPoint:CGPointMake(60, 10)];
[p addLineToPoint:CGPointMake(110, 60)];
[p addLineToPoint:CGPointMake(60, 110)];
[p addLineToPoint:CGPointMake(10, 60)];
[p closePath];
[p fill];
[p stroke];

fillColor = [UIColor redColor];
[fillColor setFill];
strokeColor = [UIColor blueColor];
[strokeColor setStroke];

p2 = [UIBezierPath bezierPath];
[p2 moveToPoint:CGPointMake(100, 10)];
[p2 addLineToPoint:CGPointMake(150, 60)];
[p2 addLineToPoint:CGPointMake(100, 110)];
[p2 addLineToPoint:CGPointMake(50, 60)];
[p2 closePath];
[p2 fill];
[p2 stroke];

CGMutablePathRef combinedPath = CGPathCreateMutableCopy(p.CGPath);
CGPathAddPath(combinedPath, NULL, p2.CGPath);
combined = [UIBezierPath bezierPathWithCGPath:combinedPath];
CFRelease(combinedPath);

mask = [CAShapeLayer new];
mask.path = combined.CGPath;

[_v_sticker.layer addSublayer:mask];
UIGraphicsEndImageContext();

但除了黑色外,我什么都没得到。我可以给图层赋予颜色但我想直接指定不同的颜色并在单层贝塞尔曲线路径中使用它们

谁能告诉我我在哪里做错了?

1 个答案:

答案 0 :(得分:0)

您的代码中存在多个错误:

  1. 您创建了一个图片上下文,但您无法访问该图片(UIGraphicsGetImageFromCurrentImageContext())。
  2. 您在遮罩层中未设置任何颜色(例如backgroundColorstrokeColor)。