0

我想每 5 秒调用一次负责在屏幕上绘制文本的方法。这是我的代码

-(void) handleTimer: (NSTimer *)timer
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 2.0); 
    CGContextSetStrokeColorWithColor(context, currentColor.CGColor); 

    CGContextTranslateCTM(context, 145.0, 240.0);
    CGContextScaleCTM(context, 1.0, -1.0); 
    CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
    CGContextSetCharacterSpacing(context, 1);
    CGContextSetTextDrawingMode(context, kCGTextFillStroke);


    CGContextSetRGBStrokeColor(context, 0.5,0.5,1,1);
    CGContextShowTextAtPoint(context, 100, 100, "01", 2);
}

但是在调用此方法 5 秒后,我收到此错误
CGContextShowTextAtPoint: invalid context

另一件事是如何显示更细的字体?

4

2 回答 2

0

据我了解,在绘制之前清除图形上下文总是一个好主意。

 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextClearRect(context, theRectInWhichYouWillBeDrawing);
于 2010-08-09T11:44:20.157 回答
0

在绘制之前,你必须一个上下文。将绘图代码放入其中drawRect会自动为您执行此操作(您drawRect在子类化 UIView 时得到)。

您可以使用 来创建自己的上下文UIGraphicsBeginImageContext(CGSize size),尽管确保您的上下文保持有效可能会出现问题。

无论如何,目前对我来说肯定是有问题的。有知道更多的人想参与进来吗?

于 2011-01-17T13:40:54.803 回答