【问题标题】:Get preview image from dae file, to display in UIImage从 dae 文件中获取预览图像,在 UIImage 中显示
【发布时间】:2019-10-18 13:03:06
【问题描述】:

我正在构建一个增强现实应用程序,并希望显示可用 DAE 文件的预览图像。

是否可以使用 Swift 从 DAE 文件中提取图像数据并将其加载到 UIImage 中?

【问题讨论】:

  • 您应该在其他软件(如 Blender)中截屏,将图像放入 UIImage 中

标签: swift uitableview uiimage arkit 3d-model


【解决方案1】:

没有开箱即用的函数可以从.dae 文件中提取屏幕截图。这并不是说没有办法在设备上执行此操作。

要了解您的要求,请考虑不同的组件。

.dae 文件是使用 ARSCNView 加载的,它是 .dae 文件的阅读器。 ARSCNView 传统上作为子视图添加到您的 ViewController。在这种情况下,您需要查看器在屏幕外渲染 3D 文件,因此您需要创建一个 ARSCNView 但不要将其添加为 VC 的子视图(我会在初始设置期间添加它以确保它全部加载,但随后注释掉该行)

将 3D 加载到 ARSCNView 后,使用以下函数将视图的上下文转换为 JPG 图像

let viewSnapshot = takeSnapshotOfView(view: yourARSCNView)

并撕下上下文并调整 JPG 的大小:

import UIKit
import CoreGraphics


func takeSnapshotOfView(view:UIView) -> UIImage? {
    UIGraphicsBeginImageContext(CGSize(width: view.frame.size.width, height: view.frame.size.height))
    view.drawHierarchy(in: CGRect(x: 0.0, y: 0.0, width: view.frame.size.width, height: view.frame.size.height), afterScreenUpdates: true)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image?.resize(CGRect(x: 0, y: 0, width: 300, height: (image?.size.height)! / 300 * (image?.size.width)!)) // resize image before returning
}


extension UIImage {
    func resize(_ toSize: CGRect) -> UIImage {
        let size = self.size

        let widthRatio = toSize.width / self.size.width
        let heightRatio = toSize.height / self.size.height

        var newSize: CGSize
        if widthRatio > heightRatio {
            newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
        } else {
            newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
        }

        let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

        UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
        self.draw(in: rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage!
    }
}

【讨论】:

    猜你喜欢
    • 2012-02-07
    • 2017-11-11
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多