gpt4 book ai didi

iOS - Core Graphics 适用于颜色,但不适用于黑色、白色、灰色

转载 作者:行者123 更新时间:2023-12-01 17:39:42 25 4
gpt4 key购买 nike

我正在使用以下类绘制一个简单的描边圆:

 @implementation StrokedCircle

- (id)initWithRadius:(CGFloat)radius strokeWidth:(CGFloat)strokeWidth strokeColor:(UIColor *)strokeColor
{
self = [super initWithRadius:radius];
if (self)
{
_strokeWidth = strokeWidth;
_strokeColor = strokeColor;
}
return self;
}


- (void)drawRect:(CGRect)rect
{
NSLog(@"Drawing with color %@ and stroke width %f", self.strokeColor, self.strokeWidth);

CGContextRef context = UIGraphicsGetCurrentContext();
CGRect circleRect = CGRectInset(rect, self.strokeWidth, self.strokeWidth);
CGContextAddEllipseInRect(context, circleRect);
CGContextSetLineWidth(context, self.strokeWidth);
CGContextSetStrokeColor(context, CGColorGetComponents([self.strokeColor CGColor]));
CGContextStrokePath(context);
}

@end

注意:父类(super class)是一个简单的圆( UIView 的子类),带有 radius设置的属性,并且 View 的背景颜色设置为 clearColor .

在 View Controller 中,我在 viewDidLoad 中添加以下代码:
 - (void)viewDidLoad
{
[super viewDidLoad];
StrokedCircle *strokedCircle = [[StrokedCircle alloc] initWithRadius:50.0 strokeWidth:1.0 strokeColor:[UIColor blueColor]];
strokedCircle.center = self.view.center;
[self.view addSubview:strokedCircle];
}

这实际上工作正常,控制台输出: 2014-06-14 10:31:58.270 ShapeTester[1445:60b] Drawing with color UIDeviceRGBColorSpace 0 0 1 1 and stroke width 1.000000屏幕中间会显示一个蓝色圆圈。

blue stroked circle

但是,当我将颜色修改为 [UIUColor blackColor] , [UIColor grayColor] , 或 [UIColor whiteColor] (但随后也更改了 View 的 backgroundColor ),不再显示圆圈。

有谁知道这种行为的原因是什么?核心图形不画灰度颜色吗?我通读了 Core Graphics Programming Guide 中的相应部分但那里没有提到这样的事情。

最佳答案

黑色、白色和灰色(由您命名的方法返回)不在 RGB 颜色空间中。它们位于灰度色彩空间中。灰度色彩空间只有一个分量(加上 alpha),而不是三个(加上 alpha)。所以你只设置了描边颜色的一个组件,而其他两个组件是未定义的。由于这个问题,您可能最终将 alpha 设置为零,因此您什么也得不到。

不要使用 CGContextSetStrokeColor .它要求您担心色彩空间(您需要使用 CGContextSetStrokeColorSpace 进行设置)。相反,请使用 CGContextSetStrokeColorWithColor ,它设置颜色空间和颜色分量:

CGContextSetStrokeColorWithColor(context, self.strokeColor.CGColor);

关于iOS - Core Graphics 适用于颜色,但不适用于黑色、白色、灰色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24218136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com