3

I am trying an image erasing code in my PanGesture. I am using the code below:

 UIImage * image  = [UIImage imageNamed:@"326d4fb72362a2e44659141cadfc4977.jpg"];
 holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 235)];
 imgForeground = [[UIImageView alloc] initWithFrame:[holderView frame]];
 [imgForeground setImage:image];
 [holderView addSubview:imgForeground];


 UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
 [panRecognizer setMinimumNumberOfTouches:1];
 [panRecognizer setMaximumNumberOfTouches:1];
 [panRecognizer setDelegate:self];
 [holderView addGestureRecognizer:panRecognizer];
 [self.view addSubview:holderView];

Then I am handling this here:

 -(void)move:(id)sender {

    CGPoint currentPoint;
    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan){
        
        lastPoint =  [sender locationInView:imgForeground];
        lastPoint.y -=20;
    }
    else{
    
        currentPoint = [sender locationInView:imgForeground];;
        currentPoint.y -=20;
    }
   
    
    UIGraphicsBeginImageContext(imgForeground.frame.size);
    [imgForeground.image drawInRect:CGRectMake(imgForeground.frame.origin.x, imgForeground.frame.origin.y, imgForeground.frame.size.width, imgForeground.frame.size.height)];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(context,kCGLineCapRound); //kCGImageAlphaPremultipliedLast);
    CGContextSetLineWidth(context, 10);
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(context, lastPoint.x, lastPoint.y);
    CGContextStrokePath(context);
    imgForeground.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

 }

How to unerase the same image?

peterh
  • 11,875
  • 18
  • 85
  • 108
Shashank Kulshrestha
  • 1,556
  • 17
  • 31
  • Is there a reason why you are building the image with an image context? This doesn't exactly solve your question but I would put my image in a scrollview. A scrollview would automatically give you scrolling if the image size is larger than viewable area. – propstm Dec 27 '12 at 13:07
  • I guess your not getting my question I am erasing the image not scrolling the image. Means when I move my finger upon image it will be removed – Shashank Kulshrestha Dec 27 '12 at 13:14
  • When you say "unerase" do you mean undo? And if so, do you need multiple levels of undo (and I suppose redo)? – bobnoble Dec 27 '12 at 14:46
  • No buddy I am using a tool where user can Erase and Unerase the picture according to his need.
    But I am not able to use unerase tool
    – Shashank Kulshrestha Dec 28 '12 at 05:11
  • @ShashankKulshrestha i updated answer please check and let me know if u face any error or queries.... – Arun Dec 31 '12 at 12:21

2 Answers2

5

I have the same issue in my app after the long search i found the simple solution for this issue.

i just used touches methods of the UIViewController

The below is the my approach,

Code:-

   #pragma mark touches stuff...



 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        lastTouch = [touch locationInView:self.editedImageView];
    }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        currentTouch = [touch locationInView:self.editedImageView];

        CGFloat brushSize;
        if (isEraser) 
        {
            brushSize=eraser;
        }
        else
        {
            brushSize=mark;
        }
        CGColorRef strokeColor = [UIColor whiteColor].CGColor;

        UIGraphicsBeginImageContext(self.editedImageView.frame.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self.editedImageView.image drawInRect:CGRectMake(0, 0, self.editedImageView.frame.size.width, self.editedImageView.frame.size.height)];
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, brushSize);
        if (isEraser) {

            CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:self.im].CGColor);
        }  
        else
        {
            CGContextSetStrokeColorWithColor(context, strokeColor);
            CGContextSetBlendMode(context, kCGBlendModeClear); 
        }
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
        CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
        CGContextStrokePath(context);
        self.editedImageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        lastTouch = [touch locationInView:self.editedImageView];
    }

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

    }

The Inputs:

self.editedImageView.image = "Your Custom image";

self.im    =   "Your Custom image";

The simple solution of your problem will be :-

CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:self.im].CGColor);

Note:

This not unerase it again drawing the image over

Update:-

 self.im    =   "Your Custom image";

this will be like this....

-(void)eraserConfigure
{
    UIImageView *resizeImage=[[[UIImageView alloc]initWithImage:editedImage]autorelease];
    /*UIImage */self.im=[UIImage imageFromView:resizeImage scaledToSize:CGSizeMake(self.editedImageView.frame.size.width, self.editedImageView.frame.size.height)];
}
Arun
  • 3,406
  • 4
  • 30
  • 55
  • @ShashankKulshrestha i hope it will help u,but it is not a unerase – Arun Dec 31 '12 at 05:45
  • Thanks spynet This code is working only problem is.... It is taking color from area wch is -50px from there – Shashank Kulshrestha Dec 31 '12 at 09:26
  • @ShashankKulshrestha sorry yar i can't get you could u elaborate so that i can understand clearly.... – Arun Dec 31 '12 at 11:47
  • when I am again drawing the image over the new image draw above -50px on y axis – Shashank Kulshrestha Dec 31 '12 at 11:51
  • Check your image view type is it aspect fit or aspect fill may be this causes this issue or u just resize accordingly – Arun Dec 31 '12 at 11:53
  • No it is UIViewContentModeScaleToFill and what contentMode I should put there – Shashank Kulshrestha Dec 31 '12 at 12:02
  • @ShashankKulshrestha try this i hope it will help you UIImageView *resizeImage=[[[UIImageView alloc]initWithImage:editedImage]autorelease]; /*UIImage */self.im=[UIImage imageFromView:resizeImage scaledToSize:CGSizeMake(self.editedImageView.frame.size.width, self.editedImageView.frame.size.height)]; – Arun Dec 31 '12 at 12:18
  • You have used two imageview here editedImageView and resizeImage... I am confused with them – Shashank Kulshrestha Dec 31 '12 at 12:33
  • And where to call eraserConfigure? – Shashank Kulshrestha Dec 31 '12 at 12:34
  • @ShashankKulshrestha don't confuse u just replace self.im = "Your Custom image"; this to UIImageView *resizeImage=[[[UIImageView alloc]initWithImage:editedImage]autorelease]; /*UIImage */self.im=[UIImage imageFromView:resizeImage scaledToSize:CGSizeMake(self.editedImageView.frame.size.width, self.editedImageView.frame.size.height)]; – Arun Dec 31 '12 at 12:45
  • This method imageFromView is not available :( – Shashank Kulshrestha Dec 31 '12 at 13:00
  • that is the image category method simple google out u can get otherwise let me know – Arun Dec 31 '12 at 13:18
  • I got this so Far is it same ? - (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { //UIGraphicsBeginImageContext(newSize); UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } – Shashank Kulshrestha Jan 02 '13 at 05:02
  • Please refer this i modified few things in the class but your method looks same if u any doubts means mail me... really i proud to help u man keep rocking Thanks for giving me 100 man – Arun Jan 02 '13 at 05:40
  • Hey dude I got another problem in same Please refer this link – Shashank Kulshrestha Feb 06 '13 at 12:36
  • @ShashankKulshrestha may i know your issue man? where is the link – Arun Feb 07 '13 at 04:09
  • Neways its resolved and the link is http://stackoverflow.com/questions/14729021/drawinrect-losing-quality-of-image-resolution/ – Shashank Kulshrestha Feb 07 '13 at 05:59
  • @ShashankKulshrestha Super nice keep rock – Arun Feb 07 '13 at 08:56
0

Save a temporary image after every drawing operation.
You should save on temporary files and limit the number of undo operations.

Alexander
  • 23,432
  • 11
  • 63
  • 73
rjobidon
  • 3,055
  • 3
  • 30
  • 36
  • Buddy I am looking for something Like Unerase when user move his finger around erased area.. Undo is something different – Shashank Kulshrestha Dec 28 '12 at 06:44
  • Its good technique but its may create problem in memory warning adding one image on other.I other hand yeah it may slow and lazy process due to writing and retrieving binary files on disk. – Tirth Dec 28 '12 at 12:27