I've written the following code in my loadView method.
//draw grid lines
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat color[4] = {.5,0.5,1.0,1.0};
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColor(context, color);
CGRect rect = CGRectMake(100.0, 200.0, 50.0, 50.0);
CGContextAddRect(context, rect);
CGContextDrawPath(context, kCGPathFillStroke);
In my app I've added some UIButtons and UILabels to a UIView. I want to draw a rectangle around the UILabels.
I've NSLogged either side of the code and get the following runtime output errors between the two logs.
<Error>: CGContextSetStrokeColor: invalid context 0x0
<Error>: CGContextAddRect: invalid context 0x0
<Error>: CGContextDrawPath: invalid context 0x0
etc.
How do I ensure I get a valid context? I assumed calling UIGraphicsGetCurrentContext(); would be sufficient.
Unless you are drawing, there is no current graphics context.
There will be a context if:
UIGraphicsContextBeginImageContextWithOptions()
or similar. In this case you usually then extract your drawing into a UIImage object for use later.