UIColor和图形上下文

tmp*_*mpz 3 uiview cgcolor uicolor ios cgcontextref

我有一个自定义的UIView,它绘制了一条这样的路径

- (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];

}
Run Code Online (Sandbox Code Playgroud)

我的自定义UIView也有一种改变填充颜色的方法

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

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

}
Run Code Online (Sandbox Code Playgroud)

如果我使用任何预定义颜色调用changeColor:方法,例如

 [UIColor redColor]  
Run Code Online (Sandbox Code Playgroud)

一切正常.相反,如果我试图给它一个自定义的颜色,如

 UIColor* color = [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f];
Run Code Online (Sandbox Code Playgroud)

我自定义UIView的颜色随机闪烁在白色,红色和蓝色之间.

任何想法为什么?

先感谢您!

sch*_*sch 6

尝试:

CGContextSetFillColorWithColor(ctx, self.color.CGColor);
Run Code Online (Sandbox Code Playgroud)

代替:

CGContextSetFillColor(ctx, CGColorGetComponents(self.color));
Run Code Online (Sandbox Code Playgroud)