为了账号安全,请及时绑定邮箱和手机立即绑定

iPhone图像拉伸(倾斜)

iPhone图像拉伸(倾斜)

慕后森 2019-11-22 15:35:57
如何歪斜图像?例如,每个角都有一个CGPoint,坐标为p1,p2,p3,p4。然后,我需要设置-p4.x + = 50,p4.y + = 30。因此,应在2D透视图中拉伸此角(p4),并使图像变形。替代文字(来源:polar-b.com)我尝试使用CATransform3D,但是似乎不能以这种方式完成,因为这只是改变视角(旋转,使一侧更近/更远)。也许CGAffineTransform有用吗?如果您知道答案,请编写示例代码。
查看完整描述

3 回答

?
慕田峪7331174

TA贡献1828条经验 获得超13个赞

我在Swift中尝试了一个很棒的答案@KennyTM,并收到一个错误“表达式太复杂,无法在合理的时间内解决”。


因此,这是Swift的简化版本:


let y21 = y2a - y1a

let y32 = y3a - y2a

let y43 = y4a - y3a

let y14 = y1a - y4a

let y31 = y3a - y1a

let y42 = y4a - y2a


let a = -H*(x2a*x3a*y14 + x2a*x4a*y31 - x1a*x4a*y32 + x1a*x3a*y42)

let b = W*(x2a*x3a*y14 + x3a*x4a*y21 + x1a*x4a*y32 + x1a*x2a*y43)

let c0 = -H*W*x1a*(x4a*y32 - x3a*y42 + x2a*y43)

let cx = H*X*(x2a*x3a*y14 + x2a*x4a*y31 - x1a*x4a*y32 + x1a*x3a*y42)

let cy = -W*Y*(x2a*x3a*y14 + x3a*x4a*y21 + x1a*x4a*y32 + x1a*x2a*y43)

let c = c0 + cx + cy


let d = H*(-x4a*y21*y3a + x2a*y1a*y43 - x1a*y2a*y43 - x3a*y1a*y4a + x3a*y2a*y4a)

let e = W*(x4a*y2a*y31 - x3a*y1a*y42 - x2a*y31*y4a + x1a*y3a*y42)

let f0 = -W*H*(x4a*y1a*y32 - x3a*y1a*y42 + x2a*y1a*y43)

let fx = H*X*(x4a*y21*y3a - x2a*y1a*y43 - x3a*y21*y4a + x1a*y2a*y43)

let fy = -W*Y*(x4a*y2a*y31 - x3a*y1a*y42 - x2a*y31*y4a + x1a*y3a*y42)

let f = f0 + fx + fy;


let g = H*(x3a*y21 - x4a*y21 + (-x1a + x2a)*y43)

let h = W*(-x2a*y31 + x4a*y31 + (x1a - x3a)*y42)

let i0 = H*W*(x3a*y42 - x4a*y32 - x2a*y43)

let ix = H*X*(x4a*y21 - x3a*y21 + x1a*y43 - x2a*y43)

let iy = W*Y*(x2a*y31 - x4a*y31 - x1a*y42 + x3a*y42)

var i = i0 + ix + iy



let epsilon = CGFloat(0.0001);

if fabs(i) < epsilon {

    i = epsilon * (i > 0 ? 1 : -1);

}


return CATransform3D(m11: a/i, m12: d/i, m13: 0, m14: g/i, m21: b/i, m22: e/i, m23: 0, m24: h/i, m31: 0, m32: 0, m33: 1, m34: 0, m41: c/i, m42: f/i, m43: 0, m44: 1.0)


查看完整回答
反对 回复 2019-11-22
  • 3 回答
  • 0 关注
  • 752 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信