title: can't erase drawn lines more than once with quartz 2d drawing app
body: <p>I'm making a draw app and what I do is to load an image from camera roll in a previous view controller, and send it to here, and load it in mainImage, then the app do some drawing in the photo. Now if I choose the eraser tool, I can erase what the app has drawn but it works only the first time I erase, because when I release my first touch and try to erase again it does nothing. Even If I choose another button to draw some other lines and then choose the eraser again, it doesn't erase. Can Anybody help me with this? </p>
  
  <pre><code>@synthesize mainImage;
  @synthesize tempDrawImage;
  
  - (void)viewDidLoad
  {
      red = 255.0/255.0;
      green = 0/255.0;
      blue = 255.0/255.0;
      brush = 10.0;
      opacity = 1.0;
      self.mainImage.image = [camViewController image]; //load photo from camera roll from previous view controller
      [super viewDidLoad];
  
      //draw line in the middle of the screen
      [self drawMiddleRectangleBrushSize:20];
  
  }
  
  -(void)drawMiddleRectangleBrushSize:(float)brushSize{
      UIGraphicsBeginImageContext(self.view.frame.size);
      [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
      CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.view.frame.size.width/2, 0);
      CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.frame.size.width/2, self.view.frame.size.height);
      CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSize );
      CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
      CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
  
      CGContextStrokePath(UIGraphicsGetCurrentContext());
      self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
      [self.tempDrawImage setAlpha:opacity];
      UIGraphicsEndImageContext();
  }
  
  - (IBAction)pencilPressed:(id)sender {
  
      UIButton * PressedButton = (UIButton*)sender;
      _eraserPress = NO;
      switch(PressedButton.tag)
      {
          case 0:
              red = 0.0/255.0;
              green = 0.0/255.0;
              blue = 0.0/255.0;
              break;
          case 1:
              red = 105.0/255.0;
              green = 105.0/255.0;
              blue = 105.0/255.0;
              break;
          case 2:
              red = 255.0/255.0;
              green = 0.0/255.0;
              blue = 0.0/255.0;
              break;
          case 3:
              red = 0.0/255.0;
              green = 0.0/255.0;
              blue = 255.0/255.0;
              break;
          case 4:
              red = 102.0/255.0;
              green = 204.0/255.0;
              blue = 0.0/255.0;
              break;
          case 5:
              red = 102.0/255.0;
              green = 255.0/255.0;
              blue = 0.0/255.0;
              break;
          case 6:
              red = 51.0/255.0;
              green = 204.0/255.0;
              blue = 255.0/255.0;
              break;
          case 7:
              red = 160.0/255.0;
              green = 82.0/255.0;
              blue = 45.0/255.0;
              break;
          case 8:
              red = 255.0/255.0;
              green = 102.0/255.0;
              blue = 0.0/255.0;
              break;
          case 9:
              red = 255.0/255.0;
              green = 255.0/255.0;
              blue = 0.0/255.0;
              break;
      }
  }
  
  - (IBAction)eraserPressed:(id)sender {
  
      red = 255.0/255.0;
      green = 255.0/255.0;
      blue = 255.0/255.0;
      opacity = 1.0;
      _eraserPress = YES;
  }
  
  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  
      mouseSwiped = NO;
      UITouch *touch = [touches anyObject];
      lastPoint = [touch locationInView:self.view];
  }
  
  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  
      mouseSwiped = YES;
      UITouch *touch = [touches anyObject];
      CGPoint currentPoint = [touch locationInView:self.view];
  
      if(_eraserPress == NO){
  
      UIGraphicsBeginImageContext(self.view.frame.size);
      [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
      CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
      CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
      CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
      CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
      CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
  
      CGContextStrokePath(UIGraphicsGetCurrentContext());
      self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
      [self.tempDrawImage setAlpha:opacity];
      UIGraphicsEndImageContext();
  
      lastPoint = currentPoint;
      }
      if(_eraserPress == YES){
          UIGraphicsBeginImageContext(self.view.frame.size);
          [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
  
          CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
          CGContextSetLineWidth(UIGraphicsGetCurrentContext(),brush);
          CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
  
          CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
          CGContextBeginPath(UIGraphicsGetCurrentContext());
          CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
          CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
          CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
          CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
          CGContextStrokePath(UIGraphicsGetCurrentContext());
          CGContextSetFlatness(UIGraphicsGetCurrentContext(), 0.1f);
          self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
  
          UIGraphicsEndImageContext();
      }
  }
  
  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  
      if(!mouseSwiped) {
          UIGraphicsBeginImageContext(self.view.frame.size);
          [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
          CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
          CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
          CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
          CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
          CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
          CGContextStrokePath(UIGraphicsGetCurrentContext());
          CGContextFlush(UIGraphicsGetCurrentContext());
          self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
      }
  
      UIGraphicsBeginImageContext(self.mainImage.frame.size);
      [self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
      [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity];
      self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
      self.tempDrawImage.image = nil;
      UIGraphicsEndImageContext();
  }
  </code></pre>