0

i am using CGContextRef to draw one horizontal bar and above-below of bar i am displaying text using drawInRect method of NSString.Here is code:

        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];

When i call second time it gives me error like:<Error>: CGContextSetFillColorWithColor: invalid context 0x0.Please help me.Thanking you.

4

1 回答 1

0

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

根据文档

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

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

于 2013-06-14T09:34:25.593 回答