无法将特定 CGColor 应用于 CGContextSetFillColor
Can't apply specific CGColor to CGContextSetFillColor
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor*aColor = [UIColor colorWithRed:2 green:191 blue:166 alpha:1];
CGContextSetFillColorWithColor(context, aColor.CGColor);
CGContextAddArc(context, rect.size.width/2, rect.size.height/2, rect.size.height/2, 0, 2*3.1415926, 0);
CGContextDrawPath(context, kCGPathFill);
}
代码是用来画圆的,但是[UIColor colorWithRed:2 green:191 blue:166 alpha:1]
不起作用,而[UIColor blackColor]
可以正常工作。
因为 + (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha
函数使用范围从 0.0
到 1.0
的 red
green
blue
alpha
属性。
你的代码应该是这样的:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor*aColor = [UIColor colorWithRed:(2.0 / 255.0) green:(191.0 / 255.0) blue:(166.0 / 255.0) alpha:1];
CGContextSetFillColorWithColor(context, aColor.CGColor);
CGContextAddArc(context, rect.size.width/2, rect.size.height/2, rect.size.height/2, 0, 2*3.1415926, 0);
CGContextDrawPath(context, kCGPathFill);
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor*aColor = [UIColor colorWithRed:2 green:191 blue:166 alpha:1];
CGContextSetFillColorWithColor(context, aColor.CGColor);
CGContextAddArc(context, rect.size.width/2, rect.size.height/2, rect.size.height/2, 0, 2*3.1415926, 0);
CGContextDrawPath(context, kCGPathFill);
}
代码是用来画圆的,但是[UIColor colorWithRed:2 green:191 blue:166 alpha:1]
不起作用,而[UIColor blackColor]
可以正常工作。
因为 + (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha
函数使用范围从 0.0
到 1.0
的 red
green
blue
alpha
属性。
你的代码应该是这样的:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor*aColor = [UIColor colorWithRed:(2.0 / 255.0) green:(191.0 / 255.0) blue:(166.0 / 255.0) alpha:1];
CGContextSetFillColorWithColor(context, aColor.CGColor);
CGContextAddArc(context, rect.size.width/2, rect.size.height/2, rect.size.height/2, 0, 2*3.1415926, 0);
CGContextDrawPath(context, kCGPathFill);
}