首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGContextRef给出错误

CGContextRef给出错误
EN

Stack Overflow用户
提问于 2013-06-14 16:52:21
回答 1查看 210关注 0票数 0

我正在使用CGContextRef绘制一个水平条形图,并在条形图的上下方向使用NSString.Here的drawInRect方法显示文本,代码如下:

代码语言:javascript
复制
        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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-14 17:34:26

错误提示上下文无效,我想第二次调用.So意味着你直接调用了这个方法。

根据文档

默认情况下,当前图形上下文为nil。在调用其drawRect:方法之前,视图对象将一个有效的上下文推送到堆栈上,使其成为当前上下文。

上下文仅在drawRect:中有效method.You需要在UIView上调用-setNeedsDisplay才能更新内容

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17104666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档