I am drawing into CGlayers, So I am creating the Layer and drawing the Layer into graphics context in drawRect method, this way
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
if(self.currentDrawingLayer == nil)
{
CGLayerRef layer = CGLayerCreateWithContext(context, bounds.size, NULL);
self.currentDrawingLayer = layer;
}
CGContextDrawLayerInRect(context, self.bounds, self.currentDrawingLayer);
}
I do all the drawing operations in my touchesMoved function
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGContextRef layerContext = CGLayerGetContext(self.currentDrawingLayer);
CGContextSetLineCap(layerContext, kCGLineCapRound);
CGContextSetLineJoin(layerContext, kCGLineJoinRound);
CGContextSetAllowsAntialiasing(layerContext, YES);
CGContextSetShouldAntialias(layerContext, YES);
CGContextSetBlendMode(layerContext,kCGBlendModeClear);
CGContextSetLineWidth(layerContext, self.eraseWidth);
CGContextBeginPath(layerContext);
CGContextAddPath(layerContext, mutablePath);
CGContextStrokePath(layerContext);
CGPathRelease(mutablePath);
[self setNeedsDisplay];
}
But when I start drawing, I get the error messages for all the CGContext functions used in touches moved, of which I am listing a few below.
<Error>: CGContextBeginPath:
This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
<Error>: CGContextAddPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
- I know the error is because, the Layer is not created yet, and because of that, I get this error, but then Layer should be created in drawRect method only because, its where the graphics context is alive.
- If I call
[self setNeedsDisplay]
first and then my drawing functions, and again at last[self setNeedsDisplay]
, then everything works fine, with no errors, but I dont think, or rather dont know wheteher this is the right thing to do.