Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIColor and Graphics Context

I have a custom UIView which draws a path like so

- (void)drawRect:(CGRect)rect{

    [[UIColor blackColor] setStroke];
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetFillColor(ctx, CGColorGetComponents(self.color));

    CGContextFillPath(ctx);

    UIBezierPath *thePath = [UIBezierPath bezierPath];
    thePath.lineWidth = _lineWidth;

    _center = CGPointMake(rect.size.width, rect.size.height);

    [thePath moveToPoint:_center];

    [thePath addArcWithCenter:_center radius:rect.size.width startAngle:M_PI endAngle:M_PI+degreesToRadians(_angle)clockwise:YES];

    [thePath closePath];
    [thePath fill];

}

My custom UIView also has a method to change the filling color

- (void) changeColor:(UIColor *) newColor {

    self.color = [newColor CGColor];
    [self setNeedsDisplay];

}

If I call the changeColor: method with any of the predefined colors such as

 [UIColor redColor]  

everything works fine. Instead if i try to give it a custom color such as

 UIColor* color = [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f];

the color of my custom UIView flicker between white, red and blue randomly.

Any ideas of why is that?

Thank you in advance!

like image 997
tmpz Avatar asked Jan 27 '26 11:01

tmpz


1 Answers

Try:

CGContextSetFillColorWithColor(ctx, self.color.CGColor);

instead of:

CGContextSetFillColor(ctx, CGColorGetComponents(self.color));
like image 94
sch Avatar answered Jan 29 '26 04:01

sch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!