Search code examples
objective-ciphonequartz-2d

CGContextShowTextAtPoint: invalid context


I want to call a method responsible for drawing text on screen after each 5 seconds. Here is my code

-(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);
}

But after 5 seconds when this method is called i am getting this error
CGContextShowTextAtPoint: invalid context

Another thing is how to show a thinner font?


Solution

  • As I understand it, it's always a good idea to clear the graphics context before drawing to it.

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