2015-05-15 29 views
0

我正在製作一個繪圖應用程序,我所做的是從前一個視圖控制器中的相機膠捲中加載圖像,並將其發送到此處,然後加載它在mainImage中,然後應用程序在照片中做一些繪圖。現在,如果我選擇橡皮擦工具,我可以清除應用程序所畫的內容,但僅在第一次擦除時才起作用,因爲當我釋放第一次觸摸並嘗試再次擦除時,它什麼也不做。即使我選擇另一個按鈕來繪製其他線條,然後再次選擇橡皮擦,它也不會擦除。任何人都可以幫助我嗎?無法用石英2d繪製應用程序多次擦除畫出的線

@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(); 
} 

回答

0

在你touchesMoved方法,當你在「擦除模式」的時候,你不更新您的lastPoint。這可能是導致你的問題的原因。

0

我已經完成了評論這兩行代碼合併兩個圖像,當兩個圖像合併到一個,你不能抹去了。希望它能幫助別人,歡呼!

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    if(!mouseSwiped) { 
    //code... 
    } 
    //self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    //self.tempDrawImage.image = nil; 
}