我正在使用CGContextRef绘制一个水平条形图,并在条形图的上下方向使用NSString.Here的drawInRect方法显示文本,代码如下:
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.Please help me.Thanking you。
发布于 2013-06-14 17:34:26
错误提示上下文无效,我想第二次调用.So意味着你直接调用了这个方法。
根据文档
默认情况下,当前图形上下文为nil。在调用其drawRect:方法之前,视图对象将一个有效的上下文推送到堆栈上,使其成为当前上下文。
上下文仅在drawRect:中有效method.You需要在UIView上调用-setNeedsDisplay才能更新内容
https://stackoverflow.com/questions/17104666
复制相似问题