【问题标题】:Image gets scale on every touch end after drawing on it - iOS图像在绘制后在每个触摸端都会缩放 - iOS
【发布时间】:2014-03-01 13:37:24
【问题描述】:

我正在使用这个 raywenderlich 教程在 Image 上绘图。该教程在背景中没有图像,但在这里我想从图库中绘制图像。

所以基本上有两个UIImageView 一个是mainImagetempDrawImage。 我正在将self.mainImage.image = randomImageFromGallery 作为我要绘制的背景图像 现在它显示得很好,绘图也发生了,但问题是图像在每次触摸结束时都会缩放。

为什么图像一直在缩放我在tempDrawImage 上没有做任何事情的代码有什么问题,所有的绘图都发生了?

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

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:self.view];

    UIGraphicsBeginImageContext(self.view.frame.size);
}

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

    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];

    CGContextRef ctxt = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctxt, lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(ctxt, currentPoint.x, currentPoint.y);
    CGContextSetLineCap(ctxt, kCGLineCapRound);
    CGContextSetLineWidth(ctxt, brush );
    CGContextSetRGBStrokeColor(ctxt, red, green, blue, 1.0);
    CGContextSetBlendMode(ctxt,kCGBlendModeNormal);

    CGContextStrokePath(ctxt);
    self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.tempDrawImage setAlpha:opacity];

    lastPoint = currentPoint;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];

    UIGraphicsEndImageContext();

    if(!mouseSwiped)
    {
        self.tempDrawImage.image = nil;
    }
}

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

    if(!mouseSwiped) {
        UIGraphicsEndImageContext();

        UIGraphicsBeginImageContext(self.view.frame.size);
        [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

    UIGraphicsBeginImageContext(self.mainImage.frame.size);
    [self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity];
    self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
    self.tempDrawImage.image = nil;
    UIGraphicsEndImageContext();
}

【问题讨论】:

  • 每个视图的框架是多少?
  • @Wain :正如我在问题中所说的,有两个 UIImageView 首先是 mainImage 宽度:316 和高度是 563 和 tempDrawImage 是 316 x 563 相同.. 添加 UIImages 的原始视图是320 x 568,视图控制器默认提供
  • @Wain :得到了提示......谢谢..它现在可以工作了,问题是两个框架的大小不同,所以它会根据当地坐标缩放..谢谢

标签: ios objective-c uiimageview uiimage touches


【解决方案1】:

检查您正在使用的所有视图的框架大小。某处存在不匹配,因此您显示的图像之一被缩放以显示。绘制图像的大小或显示的大小都需要更改。

【讨论】:

    【解决方案2】:

    如果我理解得很好,你有一张图,你想画出这张图……

    我提议:

    1. 将您的 tempDrawImage 设置为继承自 UIView 而不是 UIImageView。
    2. 在触摸动作(touchesBegan、touchesMoved、...)时将点存储在数组中
    3. 在过程中绘制 (void)drawRect:(CGRect)rect

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 2012-11-13
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多