如何使用 3D 效果调整 ios 中的图像大小。

How to resize image in ios with 3D effect.

我想调整图像大小并通过选取任意角来拉伸图像。 供参考Check Video。我想在 ios 本地使用 CGContext 或使用 BazirPath 开发相同的效果。

请让我知道如何在 ios 中做这样的事情。提供任何参考来进行此类图像操作。我的项目在 swift 3。所以这是我的首选。

如果您只想改变边角,CIPerspectiveTransform 滤镜可让您执行此操作。如果您希望能够像在 photoshop 中那样将所有网格点编辑为贝塞尔曲线路径并调整图像扭曲,那么您需要进行一些 3D 数学运算,将面片细分为三角形并将图像作为纹理应用到生成的几何图形。

无论如何,这是一个演示如何使用 CIPerspective 变换的游乐场:

    import UIKit
    import PlaygroundSupport

    let uiImage = UIImage(named: "a.png")!
    let image = CIImage(image: uiImage)!
    let filter = CIFilter(name: "CIPerspectiveTransform")!
    let topLeft = CGPoint(x: -10, y: 0)
    let bottomLeft = CGPoint(x: 100, y: uiImage.size.height)
    let topright = CGPoint(x: uiImage.size.width, y: -20)
    let bottomright = CGPoint(x: uiImage.size.width, y:uiImage.size.height)

    filter.setValue(CIVector(cgPoint:topLeft), forKey: "inputTopLeft")
    filter.setValue(CIVector(cgPoint:topright), forKey: "inputTopRight")
    filter.setValue(CIVector(cgPoint:bottomright), forKey: "inputBottomRight")
    filter.setValue(CIVector(cgPoint:bottomLeft), forKey: "inputBottomLeft")
    filter.setValue(image, forKey: kCIInputImageKey)
    let transformedImage = UIImage(ciImage: filter.outputImage!)

    PlaygroundPage.current.liveView = UIImageView(image: transformedImage)

github 上有一个可用的库:(它是用 Objective-C 开发的,但您可以在 Swift 3 项目中使用它)

如果从头开始,这种改造并不容易,即使CoreGraphic很强大,学习坡度也很难。

https://github.com/agens-no/AGGeometryKit

正如您在此处看到的,它基于 CoreGraphic,因此您可以使用 CGContext 和 UIBezierPath

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKit.h>

#import "AGKBaseDefines.h"

AGK_EXTERN_C_BEGIN

CGImageRef CGImageDrawWithCATransform3D_AGK(CGImageRef imageRef,
                                            CATransform3D transform,
                                            CGPoint anchorPoint,
                                            CGSize size,
                                            CGFloat scale) CF_RETURNS_RETAINED;

AGK_EXTERN_C_END

您可以设置水平和垂直效果:

垂直效果

let verticalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y",
type: .TiltAlongVerticalAxis)
verticalMotionEffect.minimumRelativeValue = -10
verticalMotionEffect.maximumRelativeValue = 10

水平效果

let horizontalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x",
    type: .TiltAlongHorizontalAxis)
horizontalMotionEffect.minimumRelativeValue = -10
horizontalMotionEffect.maximumRelativeValue = 10

let group = UIMotionEffectGroup()
group.motionEffects = [horizontalMotionEffect, verticalMotionEffect]
myBackgroundView.addMotionEffect(group)