Reputation: 4235
I want to fill a path with red. This is my code:
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);
I know that I have to use CGContextSetFillColor()
and CGContextFillPath()
, but it's not working.
How can I do this correctly?
Upvotes: 2
Views: 5028
Reputation: 3380
For swift use :
CGContextSetFillColorWithColor(context, UIColor.blackColor().CGColor)
CGContextFillPath(context)
Upvotes: 0
Reputation: 4235
Answer in my case is:
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);
Upvotes: 3
Reputation: 4295
You don't set a line width on the above example, so here's a sane example that will draw a line down the left hand side of a rect. Taken directly from a drawRect function
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor whiteColor] setFill];
CGContextFillRect(context, rect);
CGContextSetLineWidth(context, 2);
CGFloat gray[4] = {0.5f, 0.5f, 0.5f, 1.0f};
CGContextSetStrokeColor(context, gray);
// left
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 0, CGRectGetHeight(rect));
CGContextStrokePath(context);
Upvotes: 0