我想用红色填充路径。这是我的代码:
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextBeginPath(c);
CGContextSetStrokeColor(c, red);
CGContextSetFillColorWithColor(c, [UIColor whiteColor].CGColor);
CGContextMoveToPoint(c, 10, 10);
CGContextAddLineToPoint(c, 20, 20);
CGContextAddLineToPoint(c, 20, 40);
CGContextAddLineToPoint(c, 40, 20);
CGContextAddLineToPoint(c, 10, 10);
CGContextStrokePath(c);
CGContextFillPath(c);
我知道我必须使用 CGContextSetFillColor()
和 CGContextFillPath()
,但它不起作用。
我怎样才能正确地做到这一点?
最佳答案
我的例子的答案是:
CGContextRef context = UIGraphicsGetCurrentContext();
//set fill pattern
UIColor *patternRed = [UIColor colorWithPatternImage:[UIImage imageNamed:@"patternRed.png"]];
CGContextSetLineWidth(context, 1.0);
CGContextSetFillColorWithColor(context, patternRed.CGColor);
CGMutablePathRef pathRef = CGPathCreateMutable();
CGPathMoveToPoint(pathRef, NULL, 0, self.frame.size.height);
for (int i = 0; i<255; ++i) {
CGPathAddLineToPoint(pathRef, NULL, stepX*arrayX[i], self.frame.size.height - (arrayYRed[i]*stepY));
}
CGPathAddLineToPoint(pathRef, NULL, stepX*255, self.frame.size.height);
CGContextAddPath(context, pathRef);
CGContextFillPath(context);
CGPathRelease(pathRef);
关于objective-c - 用特定颜色填充路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10339653/