IOS中的图像变形

时间:2016-07-24 11:42:26

标签: ios swift

我是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

这是我想要的图像(当我触摸网格点图像时,我们应该包裹,如果我将网格点放在原始位置,它应该是原始的那样明智)

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

看看我对这个问题的回答:

Warp \ bend effect on a UIView?

你也可以看一下这个git库:

https://github.com/Ciechan/BCMeshTransformView

这可能是你想做的事情的一个很好的起点,但是你需要了解OpenGL,转换矩阵以及许多其他事情。

您所询问的是相当简单的OpenGL。您只需要设置一个描述修改后的网格点的三角形条。您将图像作为纹理加载,然后使用三角形条渲染纹理。

然而,“直截了当的OpenGL”有点像直接的火箭科学。高级概念可能很简单,但最终会出现很多非常繁琐的细节,以使其正常工作。

观看我使用我的应用Face Dancer

创建的这段简短视频

Face Dancer video with grid lines

答案 1 :(得分:1)

我已经为BCMeshTransformView编写了一个扩展程序,允许您应用warp变换以响应用户的触摸输入。

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代码并不完美,但它完成了我的工作。你可以玩它,但总的想法保持不变。