0

I at last managed to get DrawRect that did not clear all the time using (I also have the setClearsContextBeforeDrawing:YES )

- (void)drawRect:(CGRect)rect
{
    UIGraphicsPushContext(drawingContext);
    CGImageRef cgImage = CGBitmapContextCreateImage(drawingContext); 
    UIImage *uiImage = [[UIImage alloc] initWithCGImage:cgImage];

    CGContextSetLineWidth(drawingContext, 4.0);
    if (draw)
        CGContextSetStrokeColorWithColor(drawingContext, [UIColor  whiteColor] CGColor]);
    else
        CGContextSetStrokeColorWithColor(drawingContext, [[UIColor clearColor] CGColor]);

    CGContextMoveToPoint(drawingContext,    lastPt.x - (31.0 / self.transform.a),  lastPt.y - (31.0 / self.transform.a)  );
    CGContextAddLineToPoint(drawingContext, currPt.x - (31.0 / self.transform.a),  currPt.y - (31.0 / self.transform.a)  );
    CGContextStrokePath(drawingContext);

    UIGraphicsPopContext();
    CGImageRelease(cgImage);
    [uiImage drawInRect: rect];

    lastPt = currPt;
}

This leaves behind a line at it is drawn
My problem is when draw is NO [UIColor clearColor] is NOT erasing What am I supposed to use to erase ? Thanks in advance

BarryF
  • 77
  • 1
  • 10

1 Answers1

2

Because clearColor has an alpha of zero, drawing with it normally doesn't have any effect. You need to set the context's blend mode to kCGBlendModeCopy to make it take effect.

However, it would be simpler to just set the context's blend mode to kCGBlendModeClear when you want to erase, and back to kCGBlendModeNormal when you want to draw:

if (draw) {
    CGContextSetBlendMode(drawingContext, kCGBlendModeNormal);
    CGContextSetStrokeColorWithColor(drawingContext, [UIColor  whiteColor] CGColor]);
} else {
    CGContextSetBlendMode(drawingContext, kCGBlendModeClear);
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • @rob: using "kCGBlendModeClear" erases the drawing .. but it also clears the image set in the image view (Iam trying to draw on the image). Any ideas about this? – Shailesh Jan 23 '15 at 06:28