Ios 使用作为参数传递的UIColor设置CGContextSetStrokeColorWithColor
我的代码有一些问题。我无法将圆设置为构造函数中传递的颜色。当它运行时,我收到以下错误,“由于未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:'-CGColor未为UIColor定义;需要首先转换颜色空间。”,这是我的代码 MainViewController.mIos 使用作为参数传递的UIColor设置CGContextSetStrokeColorWithColor,ios,objective-c,quartz-graphics,uicolor,Ios,Objective C,Quartz Graphics,Uicolor,我的代码有一些问题。我无法将圆设置为构造函数中传递的颜色。当它运行时,我收到以下错误,“由于未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:'-CGColor未为UIColor定义;需要首先转换颜色空间。”,这是我的代码 MainViewController.m #import "CircleGenerator.h" CircleGenerator *newCircle =[[CircleGenerator alloc]initWithFrame:fra
#import "CircleGenerator.h"
CircleGenerator *newCircle =[[CircleGenerator alloc]initWithFrame:frame initWithColor:[UIColor blackColor];
循环发生器
@property(nonatomic, strong) UIColor *circleColor;
- (id)initWithFrame:(CGRect)frame initWithColor:(UIColor*)color;
循环发生器
- (id)initWithFrame:(CGRect)frame initWithColor:(UIColor *)color
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_circleColor = [[UIColor alloc]init];
_circleColor = color;
NSLog(@"the color is %@", _circleColor);
}
return self;
}
- (void)drawRect:(CGRect)rect
{
// Init a CGContecRef
CGContextRef context = UIGraphicsGetCurrentContext();
// Makes circle a bit smaller then the rectange to prevent it from getting cut off
CGRect border = CGRectInset(rect, 5, 5);
// Draws circle
CGContextAddEllipseInRect(context, border);
// Changes the color of the line
CGContextSetStrokeColorWithColor(context, [_circleColor CGColor]);
// Sets the line to the appropriate thickness
CGContextSetLineWidth(context, 5.0);
// Draws the circle onto the UIView
CGContextStrokePath(context);
}
CGContextSetStrokeColorWithColor(context, [_circleColor CGColor]);
我对CircleGenerator.m的以下行有问题
- (id)initWithFrame:(CGRect)frame initWithColor:(UIColor *)color
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_circleColor = [[UIColor alloc]init];
_circleColor = color;
NSLog(@"the color is %@", _circleColor);
}
return self;
}
- (void)drawRect:(CGRect)rect
{
// Init a CGContecRef
CGContextRef context = UIGraphicsGetCurrentContext();
// Makes circle a bit smaller then the rectange to prevent it from getting cut off
CGRect border = CGRectInset(rect, 5, 5);
// Draws circle
CGContextAddEllipseInRect(context, border);
// Changes the color of the line
CGContextSetStrokeColorWithColor(context, [_circleColor CGColor]);
// Sets the line to the appropriate thickness
CGContextSetLineWidth(context, 5.0);
// Draws the circle onto the UIView
CGContextStrokePath(context);
}
CGContextSetStrokeColorWithColor(context, [_circleColor CGColor]);
如果我用[UIColor blackColor]代替_circleColor,代码编译就不会有任何问题。有没有关于我做得不正确的想法?调用您的init构造函数的方法减少了颜色的保留计数。由于您通过直接指定给_circleColor来绕过颜色设置器,因此该指定不会增加保留计数-导致指针悬空 替换
\u circleColor=color代码>带有self.circleColor=color代码>更换:
CGContextSetStrokeColorWithColor(context, [_circleColor CGColor]);
与:
CGColorRef redRef = CFRetain(_circleColor.CGColor);
CGContextSetStrokeColorWithColor(context, [_circleColor CGColor]);
// use redRef and when done release it:
CFRelease(redRef)
编辑:
请参阅此链接,了解如何解决您的问题
_circleColor=[[UIColor alloc]init]
这将返回类UIPlaceholder color的实例,并且可能没有关于生成CGColor所需的颜色空间的信息
尝试\u circleColor=[UIColor clearColor]代码>
您应该使用指定的初始值设定项创建颜色,或者使用预设中的颜色,如[UIColor blackColor]。您从init方法内部的日志中得到了什么。我得到了以下结果:颜色是UIDeviceWhiteColorSpace 0 1Try删除[[UIColor alloc]init]。我猜是因为这个原因,因为它没有定义任何东西,没有颜色,没有颜色空间。删除[[UIColor alloc]init]后没有任何变化。我仍然从init方法内部的日志中收到相同的消息,并且仍然收到相同的NSInvalidArgumentException错误。您可以发布调用此方法的行吗?最初,我使用的是颜色的setter方法,我仍然有相同的问题。在此之前,CGContextSetStrokeColorWithColor(上下文,[[u circleColor CGColor]);
putNSAssert([u circleColor,@“不能为零。)
声明它不是nil。我添加了代码,它确实显示指针不是nil。控制台中没有打印任何新的内容。我仍然收到与以前相同的错误。如果我在CGContextSetStrokeColorWithColor(context,[[u circleColor CGColor])之前放置一个断点,它会显示_circleColor不是nil,(或者我认为是这样)。下面是xcode显示的内容_CircleColorUICachedDeviceWhiteColor*0x10af06800 0x000000010AF06800强烈建议不要通过self.
init
例程中的访问器来引用属性。是的,有时它似乎可以工作,但一般来说,这是个坏主意。(苹果文档)@Olie为什么?父构造函数已被调用,代码由self
上的条件检查保护。感谢您的建议,但它没有解决问题。我仍然收到与以前相同的错误。您不应该在第二行使用redRef吗?