Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.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 尝试在屏幕上绘制圆时出现严重错误_Ios_Objective C_Fatal Error_Drawrect - Fatal编程技术网

Ios 尝试在屏幕上绘制圆时出现严重错误

Ios 尝试在屏幕上绘制圆时出现严重错误,ios,objective-c,fatal-error,drawrect,Ios,Objective C,Fatal Error,Drawrect,我试图画一个圆,所以我有以下方法: - (void)drawRect:(CGRect)rect { CGRect leadingCircle = CGRectInset(rect, 2, 2);; UIBezierPath *aPath = [UIBezierPath bezierPathWithOvalInRect:leadingCircle]; CGContextRef aRef = UIGraphicsGetCurrentContext(); CGConte

我试图画一个圆,所以我有以下方法:

- (void)drawRect:(CGRect)rect {
    CGRect leadingCircle = CGRectInset(rect, 2, 2);;
    UIBezierPath *aPath = [UIBezierPath bezierPathWithOvalInRect:leadingCircle];
    CGContextRef aRef = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(aRef, 20, 20);
    CGContextSetRGBFillColor(aRef, 0.1, 0.05, 0.9, 1);
    CGContextSetRGBStrokeColor(aRef, 0, 0, 0, 1);
    CGContextRestoreGState(aRef); 
}
然后我从viewDidLoad调用它:

CGRect myRect = CGRectMake(0, 0, 60, 60);
[self drawRect:myRect];
但未绘制任何内容,控制台中显示以下消息:

CGContextSetRGBFillColor:无效的上下文0x0。这是一个严重的错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降级。此通知是出于礼貌:请解决此问题。这将成为即将进行的更新中的致命错误


提前感谢。

用此更改您的代码

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 4.0);
    CGContextSetStrokeColorWithColor(context, 
      [UIColor blueColor].CGColor);
    CGRect rectangle = CGRectMake(60,170,200,80);
    CGContextAddEllipseInRect(context, rectangle);
    CGContextStrokePath(context);
}

看起来您已将drawRect:放入UIViewController中。相反,它应该在UIView中,而不是直接调用drawRect:系统将自动调用它。并且,除非您首先调用了
CGContextSaveGState
,否则不要调用
cgContextRestoreState
。OP位于视图控制器中,而不是视图中。请检查我的更新答案。错误。正如我所说,OP位于视图控制器中,而不是视图中。