jdl
jdl

Reputation: 6323

How to fill my circle(CGContextAddArc) with a different color?

I have the following but it only draws the border of a circle. I would like to fill the circle. ??

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddArc(context, 50, 50, 50, 0, 30, 0);

//set the fill or stroke color
CGContextSetRGBFillColor(context, 1, 0.5, 0.5, 1.0);
CGContextSetRGBStrokeColor(context, 0.5, 1, 0.5, 1.0);

//fill or draw the path
CGContextDrawPath(context, kCGPathStroke);
CGContextDrawPath(context, kCGPathFill);

Upvotes: 1

Views: 5555

Answers (6)

William Hu
William Hu

Reputation: 16151

Fill path:

    let context = UIGraphicsGetCurrentContext()
    CGContextAddArc(context, self.bounds.width/2, self.bounds.height/2, 150, 0, CGFloat(2 * M_PI), 0)
    UIColor.redColor().setFill()
    CGContextFillPath(context)

circle:

        let context = UIGraphicsGetCurrentContext()
        CGContextAddArc(context, self.bounds.width/2, self.bounds.height/2, 150, 0, CGFloat(2 * M_PI), 0)
        CGContextSetLineWidth(context, 10)
        UIColor.greenColor().set()
        CGContextStrokePath(context)

Upvotes: 0

Mohamad Chami
Mohamad Chami

Reputation: 1234

if you want to fill the circle you can use this

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddArc(context, 50, 50, 50, 0, 30, 0);

CGContextSetRGBFillColor(context, 1, 0.5, 0.5, 1.0);

CGContextFillPath(context);

and this code below used to draw the border for circle

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddArc(context, 50, 50, 50, 0, 30, 0);

CGContextSetRGBStrokeColor(context, 0.5, 1, 0.5, 1.0);

CGContextStrokePath(context);

so, in this case you must to choose the first choice to fill the circle

Upvotes: 0

swathy krishnan
swathy krishnan

Reputation: 916

CGContextAddArc(ctx, x, y, 1.0, 0, 2 * Pi, 1); // Or is it 2 * M_PI?

CGContextSetFillColorWithColor(ctx, fillColor);
CGContextSetStrokeColorWithColor(ctx, strokeColor);
CGContextDrawPath(ctx, kCGPathFillStroke);;

Upvotes: 0

Shashi3456643
Shashi3456643

Reputation: 2041

For filling solid color it should have kCGPathFill in CGContextDrawPath(context, kCGPathFill);

For stroke with color it should have kCGPathStroke in CGContextDrawPath(context, kCGPathStroke);

Something like this:

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddArc(context, 50, 50, 50, 0, 30, 0);

//set the fill or stroke color
CGContextSetRGBFillColor(context, 1, 0.5, 0.5, 1.0);

//fill on drawn path
CGContextDrawPath(context, kCGPathFill);

Upvotes: 1

Deepak G M
Deepak G M

Reputation: 1650

Remove the stroke related lines and use only fill related lines.

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddArc(context, 50, 50, 50, 0, 30, 0);

//set the fill or stroke color
CGContextSetRGBFillColor(context, 1, 0.5, 0.5, 1.0);

//fill or draw the path
CGContextDrawPath(context, kCGPathStroke);

If you want only a circle

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddArc(context, 50, 50, 50, 0, 30, 0);

CGContextSetRGBStrokeColor(context, 0.5, 1, 0.5, 1.0);
CGContextDrawPath(context, kCGPathStroke);

Upvotes: 1

Nathan Day
Nathan Day

Reputation: 6037

You need to use CGContextFillPath to fill the path.

Upvotes: 2

Related Questions