【问题标题】:Converting UIView with perspective transform to UIImage将具有透视变换的 UIView 转换为 UIImage
【发布时间】:2016-08-13 20:25:06
【问题描述】:

我想将带有透视变换的 UIView 转换为 UIImage,但我得到了没有变换的图像。如何保持转型?

我正在使用以下代码为 UIView 进行透视变换:

    var rotationAndPerspectiveTransform = CATransform3DIdentity
    rotationAndPerspectiveTransform.m34 = 1.0 / -500
    rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 45.0 * CGFloat(M_PI) / 180.0, 1.0, 0.0, 0.0)
    rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 45.0 * CGFloat(M_PI) / 180.0, 0.0, 1.0, 0.0)
    self.photoImageView.layer.transform = rotationAndPerspectiveTransform

然后将具有透视变换的 UIView 转换为 UIImage:

    UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
    view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

结果我得到了正确的图像,但没有透视

【问题讨论】:

  • @LeoDabus 同样的结果
  • @LeoDabus UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0) photoImageView.layer.presentationLayer()!.renderInContext(UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext UIGraphicsEndImageContext() 没有帮助

标签: ios swift uiview uiimage core-animation


【解决方案1】:

您可以使用 UIView 方法 drawViewHierarchyInRect。它会将整个视图层次结构的快照呈现为在屏幕上可见的当前上下文。

试试这样:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

【讨论】:

  • @AntonVodolazkyi 我已经测试过了,它确实有效。你做错了什么。我将发布一个示例项目的链接
  • 我将等待您的回复
  • 我做了,但还是一样,这里是截图:link
  • 你需要渲染视图
  • 非常感谢!你节省了我很多时间!
猜你喜欢
  • 1970-01-01
  • 2018-01-09
  • 2011-07-25
  • 2012-12-25
  • 1970-01-01
  • 1970-01-01
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多