2010-05-26 36 views
0

我想调用一个方法负责每隔5秒后在屏幕上绘制文本。这里是我的代码CGContextShowTextAtPoint:无效的上下文

-(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后:无效的情况下

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

+0

如何格式化代码,使其在stackoverflow中看起来很完美? – coure2011 2010-05-26 18:31:00

回答

0

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

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextClearRect(context, theRectInWhichYouWillBeDrawing); 
0

你得绘制到之前的上下文。把绘图代码放在drawRect里面会自动为你做(当你继承UIView的时候你会得到drawRect)。

您可以使用UIGraphicsBeginImageContext(CGSize size)创建自己的上下文,但它可能会出现问题,请确保您的上下文保持有效状态。

无论如何,对我来说当然是有问题的。任何人都知道更多关于这个想要权衡?

相关问题