CGContextStrokePath扩展问题

时间:2013-08-04 14:41:56

标签: iphone uiimageview uiimage cgcontext

我正在制作一个有两个 UIImageViews 的绘图应用程序。我想要做的是擦除顶部的UIImageView,以便我可以看到底部的UIImageView,反之亦然。我已经实现了所有的功能和它的完美工作,但问题发生在我使用 UIGestureRecognizer 平移/缩放UIImageView并且我开始擦除图像,因此LineWidth也是Zoomed例如:initialy我的橡皮擦宽度设置为25.0f如果我缩放我的UIImageView 200%我的橡皮擦也缩小到200%,即从25.0f到50.0f这使得一切都变得疯狂。所以我的问题是如何将我的橡皮擦保留到其原始宽度而不是缩小/缩小图像缩放?

这是我的代码:

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

    UITouch *touch = [touches anyObject];

    if (selectedButton == self.Image1Btn)
        currentPoint = [touch locationInView:self.ImageCaptureView];
    if (selectedButton == self.Image2Btn)
        currentPoint = [touch locationInView:self.ImageCaptureView];

    if (bottomBarSelectedButton == 1)
    {        
        if (selectedButton == self.Image1Btn)
        {
            UIGraphicsBeginImageContext(self.Image1.frame.size);
            [self.Image1.image drawAtPoint:CGPointZero];

        }
        if (selectedButton == self.Image2Btn)
        {
            UIGraphicsBeginImageContext(self.Image2.image.size);
            [self.Image2.image drawAtPoint:CGPointZero];

        }

        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, self.lineWidth);
        CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
        CGContextSetBlendMode(context, kCGBlendModeClear);
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, previousPoint1.x, previousPoint1.y-36);
        CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y-36);
        CGContextStrokePath(context);


        if (selectedButton == self.Image1Btn)
        {
            self.Image1.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            previousPoint1 = [touch locationInView:self.ImageCaptureView];
        }
        if (selectedButton == self.Image2Btn)
        {
            self.Image2.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            previousPoint1 = [touch locationInView:self.ImageCaptureView];
        }
    }
}

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {

    CGAffineTransform t = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.view.transform = t;
    recognizer.scale = 1;

}

0 个答案:

没有答案