Mateo E
Mateo E

Reputation: 39

How To Erase Image With Touch

Right now what I can do is draw with touch, using touchesMoved.

I will provide my necessary code, but I ALSO ASK FOR SOMEONE to share their knowledge and their code because this has really got me stuck! Too be honest, Im not one of those cheap people who nag for code then do command-c and then command-v into XCode. Explanations are always welcome and benefit my learning as that is still something I am undergoing at the age of 12!

Here is all my code that is related to the question:

.h:

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface GameScreen : UIViewController  {

   BOOL mouseSwiped;
   float lineWidth;
   CGPoint lastPoint;
   IBOutlet UIImageView *drawImage;





   CGFloat f1;
   CGFloat f2;
   CGFloat f3;
   CGFloat f4;



}


-(IBAction)cyan;
-(IBAction)blue;
-(void)checktouch;
-(IBAction)eraser;
-(void)checkgreen;
-(IBAction)orange;
-(IBAction)purple;
-(IBAction)pink;
-(IBAction)black;
-(void)mouseswiped;
-(IBAction)clear;
-(IBAction)dismiss;
-(IBAction)green;
-(IBAction)yellow;
-(IBAction)brown;
-(IBAction)grey;
-(IBAction)white;
-(IBAction)newdp;
-(IBAction)menu;
-(void)remove;
-(IBAction)red;

@end

.m:

#import "GameScreen.h"
#import "DoodlePicsViewController.h"
#import "Settings.h"

@implementation GameScreen



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



   mouseSwiped = NO;
   UITouch *touch = [touches anyObject];
   lineWidth = thickness.value;

   lastPoint = [touch locationInView:self.view];
   lastPoint.y -= 20;

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
   mouseSwiped = YES;

   UITouch *touch = [touches anyObject]; 
   CGPoint currentPoint = [touch locationInView:self.view];
   currentPoint.y -= 20;


   UIGraphicsBeginImageContext(self.view.frame.size);
   [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

   CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
   CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
   CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),f1, f2, f3, f4);
   CGContextBeginPath(UIGraphicsGetCurrentContext());
   CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
   CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
   CGContextStrokePath(UIGraphicsGetCurrentContext());
   drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();


   lastPoint = currentPoint;
}









-(IBAction)yellow {

   f1 = 1.0;
   f2 = 1.0;
   f3 = 0.0;
   f4 = 1.0;

}
-(IBAction)brown {

   f1 = 0.6;
   f2 = 0.4;
   f3 = 0.2;
   f4 = 1.0;

}
-(IBAction)white {

   f1 = 1.0;
   f2 = 1.0;
   f3 = 1.0;
   f4 = 1.0;

}
-(IBAction)grey {

   f1 = 0.5;
   f2 = 0.5;
   f3 = 0.5;
   f4 = 1.0;

}
-(IBAction)red {

   f1 = 1.0;
   f2 = 0.0;
   f3 = 0.0;
   f4 = 1.0;

}

-(IBAction)cyan {


   f1 = 0.0;
   f2 = 1.0;
   f3 = 1.0;
   f4 = 1.0;
}

-(IBAction)blue {


   f1 = 0.0;
   f2 = 0.0;
   f3 = 1.0;
   f4 = 1.0;
}
-(IBAction)black {


   f1 = 0.0;
   f2 = 0.0;
   f3 = 0.0;
   f4 = 1.0;
}
-(IBAction)green {

   f1 = 0.0;
   f2 = 1.0;
   f3 = 0.0;
   f4 = 1.0;

}

-(IBAction)orange {


   f1 = 1.0;
   f2 = 0.5;
   f3 = 0.0;
   f4 = 1.0;
}
-(IBAction)pink {


   f1 = 1.0;
   f2 = 0.0;
   f3 = 1.0;
   f4 = 1.0;
}
-(IBAction)purple {

   f1 = 0.5;
   f2 = 0.0;
   f3 = 0.5;
   f4 = 1.0;

}




-(IBAction)eraser {

//I'm stuck right here LOL!

}



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

   UITouch *touch = [touches anyObject];
   if(!mouseSwiped) {
       UIGraphicsBeginImageContext(self.view.frame.size);
       [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
       CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
       CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
       CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), f1, f2, f3, f4);
       CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
       CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
       CGContextStrokePath(UIGraphicsGetCurrentContext());
       CGContextFlush(UIGraphicsGetCurrentContext());
       drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
       UIGraphicsEndImageContext();
   }}
-(void)viewDidLoad {


   f1 = 1.0;
   f2 = 0.0;
   f3 = 0.0;
   f4 = 1.0;




}
@end

Upvotes: 2

Views: 3523

Answers (3)

Skovie
Skovie

Reputation: 197

I have put two image views on top of one another, and I load the background in one and draw in the other, I set the background to opacity 0% on the draw image view, the I can just do:

-(IBAction) eraseLine {

drawImage.image = nil;

}

that removes all lines without removing the image i have in the background view.

Upvotes: 0

omz
omz

Reputation: 53551

As you correctly observed, drawing with the clear color doesn't have any effect when you draw with the default blend mode. When you set your graphics context's blend mode to "copy" however, everything that you draw will replace what's underneath it instead of painting over it. Drawing with the clear color will then have the effect of an eraser.

CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);

Upvotes: 2

Yo_Its_Az
Yo_Its_Az

Reputation: 2073

You can try making the eraser just a normal touchesMoved and set the color to whatever background color you are using. So when they color, it will look like it is erasing but it is in fact just coloring with the same color as the background. You seem to have the code already so I don't think I need to post it. Hope this helps.

Upvotes: 0

Related Questions