如何取消擦除已擦除的 UIImage?

How to unerase an erased UIImage?(如何取消擦除已擦除的 UIImage?)

本文介绍了如何取消擦除已擦除的 UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 PanGesture 中使用图像擦除代码.我正在使用下面的代码:

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];

然后我在这里处理:

 -(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?

推荐答案

经过长时间的搜索,我在我的应用中遇到了同样的问题,我找到了解决这个问题的简单方法.

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

我只是用了UIViewController的touches方法

i just used touches methods of the UIViewController

以下是我的方法,

代码:-

   #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
    {

    }

输入:

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

self.im    =   "Your Custom image";

您的问题的简单解决方案是:-

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

注意:

这不会取消它再次绘制图像

This not unerase it again drawing the image over

更新:-

 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)];
}

这篇关于如何取消擦除已擦除的 UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何取消擦除已擦除的 UIImage?

基础教程推荐