CGContextRef给出错误

时间:2013-06-14 08:52:21

标签: ios cgcontextref

我正在使用CGContextRef绘制一个水平条和条形图的上方 - 我正在使用drawInRect NSString方法显示文字。以下是代码:

        CGContextRef context = UIGraphicsGetCurrentContext();//set frame for bar
        CGRect frame;
        frame.origin.x = prevWidth;
        frame.origin.y = heightBar;
        frame.size.height = heightOfBar;
        frame.size.width = 10;

        UIColor* color = [ColorArray objectAtIndex:i];
        CGContextSetFillColorWithColor(context, color.CGColor);
        CGContextSetLineWidth(context, lineWidth);
        CGContextSetStrokeColorWithColor(context, borderColor.CGColor);

        CGContextFillRect(context, frame);
        CGContextStrokeRect(context, frame);
        [nameString drawInRect:frame1 withFont:font lineBreakMode:NSLineBreakByClipping alignment:NSTextAlignmentCenter];

当我第二次打电话时,它会给我一个错误:<Error>: CGContextSetFillColorWithColor: invalid context 0x0。请帮助我。谢谢你。

1 个答案:

答案 0 :(得分:0)

错误说上下文无效。所以我想第二次调用就意味着你直接调用了这个方法。

根据文档

  

默认情况下,当前图形上下文为零。在致电之前   drawRect:方法,视图对象将有效的上下文推送到堆栈上,   让它成为现实。

上下文仅在drawRect:方法中有效。您需要在UIView上调用-setNeedsDisplay来更新内容