Search code examples
objective-cquartz-graphicsquartz-2d

I have simple questions about drawing and erasing lines in quartz framework


I am trying to draw and erase lines by quartz framework.

But I'm facing some problems.

Please advice.

Q1) I have made follow method and excute the method if press UIButton.

- (void)drawCustom {

    cgc_Context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(cgc_Context, 5.0);
    CGContextSetStrokeColorWithColor(cgc_Context, [UIColor yellowColor].CGColor);
    CGContextMoveToPoint(cgc_Context, 0, 0);
    CGContextAddLineToPoint(cgc_Context, 200, 50);
    CGContextStrokePath(cgc_Context);
}

But result I've got is :

suckjuui-iMac.local QuartzTest[5008] : CGContextSetLineWidth: invalid context 0x0 Jul 20 20:22:50

suckjuui-iMac.local QuartzTest[5008] : CGContextSetStrokeColorWithColor: invalid context 0x0 Jul 20 20:22:50

suckjuui-iMac.local QuartzTest[5008] : CGContextMoveToPoint: invalid context 0x0 Jul 20 20:22:50

suckjuui-iMac.local QuartzTest[5008] : CGContextAddLineToPoint: invalid context 0x0 Jul 20 20:22:50

suckjuui-iMac.local QuartzTest[5008] : CGContextDrawPath: invalid context 0x0

Why does message occur like above?

Q2) How to erase lines drawn?


Solution

  • For the second question.

    You can't erase lines, because as per wattson12 your drawing needs to occur in drawRect. You can only re-draw the graphics (minus what you wanted to erase).