How to Unerase the erased UIImage

前端 未结 2 651
忘掉有多难
忘掉有多难 2020-12-10 21:17

Hi Guys I am trying a Image erasing code in my PanGesture I am using code below:

 UIImage * image  = [UIImage imageNamed:@\"326d4fb72362a2e44659141cadfc4977         


        
相关标签:
2条回答
  • 2020-12-10 21:38

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

    0 讨论(0)
  • 2020-12-10 21:45

    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)];
    }
    
    0 讨论(0)
提交回复
热议问题