【问题标题】:How to resize image in ios with 3D effect.如何在 ios 中使用 3D 效果调整图像大小。
【发布时间】:2017-03-23 08:54:17
【问题描述】:

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

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

【问题讨论】:

  • 查看我的回答to this question。这是同一件事,但问题是关于使用 Windows GDI 而不是 iOS 的核心图形。但是,答案与平台无关。
  • @user1118321 在您的答案中图像拉伸是正确的。但我想要源代码或openGL中的任何参考。

标签: ios iphone image-processing swift3 image-resizing


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    如果您只想改变角落,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)
    

    【讨论】:

      【解决方案3】:

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

      垂直效果

      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)
      

      【讨论】:

        猜你喜欢
        • 2013-09-05
        • 2013-06-05
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 2012-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多