Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用作为参数传递的UIColor设置CGContextSetStrokeColorWithColor_Ios_Objective C_Quartz Graphics_Uicolor - Fatal编程技术网

Ios 使用作为参数传递的UIColor设置CGContextSetStrokeColorWithColor

Ios 使用作为参数传递的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

我的代码有一些问题。我无法将圆设置为构造函数中传递的颜色。当它运行时,我收到以下错误,“由于未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:'-CGColor未为UIColor定义;需要首先转换颜色空间。”,这是我的代码

MainViewController.m

#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]);
put
NSAssert([u circleColor,@“不能为零。)
声明它不是nil。我添加了代码,它确实显示指针不是nil。控制台中没有打印任何新的内容。我仍然收到与以前相同的错误。如果我在
CGContextSetStrokeColorWithColor(context,[[u circleColor CGColor])之前放置一个断点,它会显示_circleColor不是nil,(或者我认为是这样)。下面是xcode显示的内容_CircleColorUICachedDeviceWhiteColor*0x10af06800 0x000000010AF06800强烈建议不要通过
self.
init
例程中的访问器来引用属性。是的,有时它似乎可以工作,但一般来说,这是个坏主意。(苹果文档)@Olie为什么?父构造函数已被调用,代码由
self
上的条件检查保护。感谢您的建议,但它没有解决问题。我仍然收到与以前相同的错误。您不应该在第二行使用redRef吗?