【问题标题】:Create Scalable Path CGContextPath创建可伸缩路径 CGContextPath
【发布时间】:2013-09-26 09:28:23
【问题描述】:

我是 iOS 开发新手。

我在使用 Core Graphics/UIKit 绘图时遇到问题。 我想在Window中实现一个类似绘画形状的功能。

我使用这个来源:https://github.com/JagCesar/Simple-Paint-App-iOS,并添加新功能。

当 touchesMoved 时,我根据 touchesBegan 时的点和当前的触摸点绘制一个形状。它绘制了所有的形状。

    - (void)drawInRectModeAtPoint:(CGPoint)currentPoint
    {
    UIGraphicsBeginImageContext(self.imageViewDrawing.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    [self.currentColor setFill];

    [self.imageViewDrawing.image drawInRect:CGRectMake(0, 0, self.imageViewDrawing.frame.size.width, self.imageViewDrawing.frame.size.height)];

    CGContextMoveToPoint(context, self.beginPoint.x, self.beginPoint.y);
    CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
    CGContextAddLineToPoint(context, self.beginPoint.x * 2 - currentPoint.x, currentPoint.y);
    CGContextAddLineToPoint(context, self.beginPoint.x, self.beginPoint.y);
    CGContextFillPath(context);

    self.currentImage = UIGraphicsGetImageFromCurrentImageContext();
    self.imageViewDrawing.image = self.currentImage;
    UIGraphicsEndImageContext();
    }

我的意思是,我只想创建一个形状,当touchesBegan时,应用程序记录点,当touchesMoved时,形状被触摸缩放,当touchesEnd时,将形状绘制到ImageContex

希望你能给我一些建议。 谢谢。

【问题讨论】:

  • 所以你的意思是要从触摸开始拖到结束,每次获得更新时调整图像大小,然后将最终图像保存到最后的上下文中?
  • 我的意思是,我只想创建一个形状,当touchesBegan时,app记录点,当touchesMoved时,通过touches缩放形状,当touchesEnd时,将形状绘制到ImageContext中跨度>

标签: ios core-graphics cgcontextdrawimage


【解决方案1】:

您可能希望从上下文中提取此功能。当您使用图像时,请使用图像视图。在触摸开始时,创建图像和图像视图。将图像视图框架设置为尺寸为 {1, 1} 的触摸点。随着触摸的移动,通过更改其frame 来移动/缩放图像视图。当触摸结束时,使用起点和终点将图像渲染到上下文中(应该与图像视图的最终frame 相同)。

这样做意味着您不会在上下文中添加任何需要在收到下一次触摸更新时再次删除的内容。上述方法同样适用于CALayer,而不是图像视图。您还可以在视图上使用transform 查看解决方案。

【讨论】:

  • 谢谢你的建议,我会努力的。
猜你喜欢
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多