Erasing with CGContext

Discussion in 'iOS Programming' started by spaceman816, Jun 7, 2010.

  1. spaceman816 macrumors member

    Joined:
    Jul 29, 2009
    #1
    I've looked all over for this but nobody seems to have posted a working answer:
    How can I make an eraser for a drawing made using CGContext?
    I can't just draw the background color over the drawing because I need the background to remain transparent.
    People have suggested using transparent paint, but that won't work.
    I need to replace what's drawn with transparent paint.

    Thanks.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You need to set the CGBlendMode correctly. I have code that does this (draws a colour then overdraws in transparent) but I'm not at home right now so I can't confirm which blend mode I am using (or that this is what I am doing).
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    CGContextClearRect() might work, but not sure if it would work along with a clipping (for erasing non-rectangles). I would assume it would but haven't tested.
     
  4. spaceman816 thread starter macrumors member

    Joined:
    Jul 29, 2009
    #4
    I meant to mention that I tried this out, but it's a really ugly square eraser that doesn't fit my needs. I need a round cap.
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    Trying creating a path, adding it to the context, and using CGContextClip() to clip, then do the erase.
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    And, of course, you've investigated the solution I proposed? Thought not.

    This is what I do:

    Code:
    	CGContextSetStrokeColorWithColor(c, [[UIColor clearColor] CGColor]);
    	CGContextSetFillColorWithColor(c, [[UIColor clearColor] CGColor]);
    	CGContextSetBlendMode(c, kCGBlendModeClear);
    	CGContextAddEllipseInRect(c,CGRectMake(size.width-(size.width/2)-(size.width/10), (size.width/10), (size.width/2), (size.height/2)));
    	CGContextClosePath(c);
    	CGContextDrawPath(c, kCGPathFillStroke); 
    
     
  7. spaceman816 thread starter macrumors member

    Joined:
    Jul 29, 2009
    #7
    I hadn't gotten a chance to look into it- but this works perfectly.
    Thanks so much.
     
  8. spaceman816 thread starter macrumors member

    Joined:
    Jul 29, 2009
    #8
    the eraser actually jumps a little too much (if you drag your finger quickly, a circle gets erased here and there with space left in the middle). is there any way to smooth the path?
    - sorry if i'm asking dumb questions...im new to objective c and haven't been able to find anything that fixes this.
     
  9. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    You will need to track the path and interpolate between the points you are actually picking up to provide a smooth path. There is no built in way to do this: it's 100% up to you.
     

Share This Page