从具有 alpha 分量的 UIColor 到 CGColor 的转换

Conversion from UIColor with alpha component to CGColor

这段代码工作正常

self.fillColor = [UIColor whiteColor].CGColor;

虽然这会在我的 drawRect 函数中引发异常

self.fillColor = [[UIColor whiteColor] colorWithAlphaComponent:0.2].CGColor;

绘制矩形:

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, self.fillColor); //exception here
    CGContextSetStrokeColorWithColor(context, self.fillColor);

    CGContextFillEllipseInRect(context, rect);
    CGContextStrokeEllipseInRect(context, rect);

}

有什么问题?

好的。看起来错误来自 属性 的定义方式。

试试这样定义...

@property UIColor *fillColor;

然后在drawRect中你可以使用...

fillColor.CGColor

我已经做了几百次了。 Alpha 值没有任何区别。