【问题标题】:iOS CGAffineTransform with Masking带有屏蔽的 iOS CGAffineTransform
【发布时间】:2018-05-08 12:27:56
【问题描述】:

我目前正在开发一个 iOS 应用程序,您可以在其中处理图像。 (旋转、缩放、平移)。我使用一个 uiimageview 我添加了手势。这工作正常,但我也有一些固定大小的遮罩矩形。 Initial State

处理完图像后,我想要蒙版矩形内的内容。 我还想要处理后图像的遮罩矩形的四个边缘点。

我知道我必须以某种方式将 imageview 变换应用于点,但它不起作用。

    let points = maskView.edgePoints()

    let translateTransform = CGAffineTransform(translationX: translationPoint.x, y: translationPoint.y)
    let rotateTransform = CGAffineTransform(rotationAngle: CGFloat(rotationAngle))
    let scaleTransform = CGAffineTransform(scaleX: xScale, y: yScale)

    let finalTransform = rotateTransform.concatenating(scaleTransform).concatenating(translateTransform)

    let topleftPoint = points[0].applying(finalTransform)
    let toprightPoint = points[1].applying(finalTransform)
    let bottomleftPoint = points[2].applying(finalTransform)
    let bottomrightPoint = points[3].applying(finalTransform)

边缘点结果:Sample

左上角:(50.75, -8.75)

右上角:(63.6072332330383,-365.252863911729)

左下角:(-172.064289944831,-16.7857707706489)

右下角:(-159.207056711792, -373.288634682378)

但左上角应该类似于 (0,0) 和左下角类似 (40,200)?

也许你可以给我一些提示或有用的链接!

提前谢谢!

【问题讨论】:

  • 你想裁剪那个黑色矩形下面的部分吗?
  • 你可以设置clip绑定为true
  • 我不知道你到底想做什么?以及为什么要将转换应用于 maskView.edgePoints ?你想让你的黑色边框相应地移动:?
  • 我认为需要更多信息。 points 是否包含视图的framebounds?如果是框架,那么左上角没有理由应该是 0,0。您可能还会得到不连贯的结果,除非您应用变换的 anchorPointpoints 的匹配。这也是缺失的信息。
  • 黑色矩形保持稳定。红苹果卡片可以缩放、缩放和平移。完成图像处理后,imageview 具有特定的变换矩阵。我想知道图像视图的裁剪矩形。初始位置:(Topleft, Topright, bottomleft, bottomright): ((0,0), (0,280), (180, 0), (180, 280))。例如当我将图像视图向下移动 50 像素时,蒙版的边缘点应该是((0,50),(0,320),(180,50),(180,320))。当我只做翻译时,它可以工作。但不结合缩放和旋转。

标签: ios swift masking cgaffinetransform


【解决方案1】:

问题在于您的转换顺序。现在你的变换顺序是 Rotate Scale Translate,它应该是 Scale Rotate Translate。

let finalTransform = scaleTransform.concatenating(rotateTransform).concatenating(translateTransform)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多