2012-08-01 33 views
0

我正在研究一些图像处理项目,我需要在图像上实现橡皮擦。我还必须实现捏合效果以缩小或缩小图像。 Pinch工作很好。橡皮擦也完美如果我不通过捏缩放图像。但是当我使用捏,然后使用橡皮擦。图像变得模糊。我在UIPanGestureRecognizer上实施了橡皮擦。如何在objective-c iPhone中缩放图像前后使用橡皮擦?

下面是橡皮的代码。

CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender locationInView:tattooImage]; 
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) 
{ 
    lastPoint = translatedPoint; 
    //lastPoint.x += 60; 
    //lastPoint.y += 60; 
} 
else 
{ 
    CGPoint currentPoint = translatedPoint; 
    //currentPoint.x += 60;  
    //currentPoint.y += 60; 

    UIGraphicsBeginImageContext(tattooImage.frame.size); 
    [tattooImage.image drawInRect:CGRectMake(0, 0,tattooImage.frame.size.width, tattooImage.frame.size.height)]; 
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear); 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25.0); 

    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()) ; 

    tattooImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 
} 

请提供任何帮助。提前致谢。

回答

1

我找到了解决办法。我做了一些小的改变,并得到了理想的结果。我在代码中的任何地方都改变了tattooImage.frame.sizetattooImage.bounds.size,并且它完美地工作。由于https://stackoverflow.com/users/1389202/pete-c

0

我想我有一个解决方案。首先,您使用UIPanGestureRecognizer移动物体。你可以简单地通过我们UITouch,因为执行会更容易。我提供了一些代码: -

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

    mouseSwiped = YES; 

    UITouch *touch = [[event touchesForView:imageView] anyObject]; 
    CGPoint currentPoint = [touch locationInView:drawingView]; 

    UIGraphicsBeginImageContext(drawingView.frame.size); 
    [drawingView.image drawInRect:CGRectMake(0, 0, drawingView.frame.size.width, drawingView.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
    if (eraserSelected) 
    { 
     CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0); 
    } 
    [self changeBrushColor]; 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext() , lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()) ; 
    drawingView.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 
    } 

现在你可以让你自己touchesBegantouchesEnded方法。

+0

的问题是不是与移动或用橡皮擦。问题是同时使用缩放和橡皮擦。你能提供关于这个的解决方案吗? – 2012-08-01 08:18:02

4

图像操作程序的工作方式不是直接在屏幕上看到的东西(除了MSPaint ...),而是通过将所有工作应用到1:1的比例副本,并显示缩放的图像在那个版本。

你需要做的是保持你存储关闭屏幕上的工作形象。屏幕上显示的图像需要从此源复制。

然后可以计算所有的触摸事件的发生有关的图像比较,而不是应用你的擦除/直接绘制的图像在屏幕上,并将其应用到适当缩放未放大图像。

线沿线的东西:

  1. 图像的存储副本工作。
  2. 获取触摸位置。
  3. 基于尺度/位置鉴于
  4. 应用橡皮擦计算相对位置在当前缩放级别/位置接触到上的图像的屏幕拷贝
  5. 更新图像的存储的副本。