我是 IOS 图像编辑工作的新手。
我想在 ios 或 swift(任何一个)中实现 Image Warp 的功能。
我搜索了很多谷歌但没有得到我想要的东西
下面的链接我是searf
How can you apply distortions to a UIImage using OpenGL ES?
https://github.com/BradLarson/GPUImage
https://github.com/Ciechan/BCMeshTransformView
这是我想要的图像(当我触摸网格点图像时,我们应该换行,如果我将网格点放在原始位置,它应该是原始的)
最佳答案
我已经为 BCMeshTransformView 编写了一个扩展,允许您应用扭曲变换来响应用户的触摸输入。
extension BCMutableMeshTransform {
static func warpTransform(from startPoint:CGPoint,
to endPoint:CGPoint, in size:CGSize) -> BCMutableMeshTransform {
let resolution:UInt = 30
let mesh = BCMutableMeshTransform.identityMeshTransform(withNumberOfRows: resolution,
numberOfColumns: resolution)!
let _startPoint = CGPoint(x: startPoint.x/size.width, y: startPoint.y/size.height)
let _endPoint = CGPoint(x: endPoint.x/size.width, y: endPoint.y/size.height)
let dragDistance = _startPoint.distance(to: _endPoint)
for i in 0..<mesh.vertexCount {
var vertex = mesh.vertex(at: i)
let myDistance = _startPoint.distance(to: vertex.from)
let hEdgeDistance = min(vertex.from.x, 1 - vertex.from.x)
let vEdgeDistance = min(vertex.from.y, 1 - vertex.from.y)
let hProtection = min(100, pow(hEdgeDistance * 100, 1.5))/100
let vProtection = min(100, pow(vEdgeDistance * 100, 1.5))/100
if (myDistance < dragDistance) {
let maxDistort = CGPoint(x:(_endPoint.x - _startPoint.x) / 2,
y:(_endPoint.y - _startPoint.y) / 2)
let normalizedDistance = myDistance/dragDistance
let normalizedImpact = (cos(normalizedDistance * .pi) + 1) / 2
vertex.to.x += maxDistort.x * normalizedImpact * hProtection
vertex.to.y += maxDistort.y * normalizedImpact * vProtection
mesh.replaceVertex(at: i, with: vertex)
}
}
return mesh
}
}
然后只需将该属性设置为您的 transformView。
fileprivate var startPoint:CGPoint?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
startPoint = touch.location(in: self)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let startPoint_ = startPoint else { return }
guard let touch = touches.first else { return }
let position = touch.location(in: self)
transformView.meshTransform = BCMutableMeshTransform.warpTransform(from: startPoint_,
to: position, in: bounds.size)
}
warp 代码并不完美,但在我的案例中它完成了工作。您可以使用它,但总体思路保持不变。
关于ios - IOS 中的图像变形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38551631/