是否可以将运行时从服务器恢复的图像加载到平面对象中?

Is it possible to load images recovered from a server in runtime into a plane object?

我被要求构建一个应用程序来显示带有 AR 的目录,所以我需要做的很简单:当用户选择产品时,我必须将 base64 中恢复的图像从服务器加载到平面中目的。 swift - arkit 可以吗?或者所有的 sprites/images/textures 都需要预先加载到 assets 文件夹中?

您绝对可以从服务器下载资源,将它们保存到设备(例如在 NSDocumentsDirectory 中),然后加载文件 URL。我这样做是为了一个与你类似的用例——至少听起来是这样,根据你给出的描述——

编辑 这是相关的代码。我使用 Alamofire 从服务器下载并使用 ZIPFoundation 进行解压缩。我相信如果你只需要下载一张图片,会简单一些,可能不需要解压部分。

let modelsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

func loadNodeWithID(_ id: String, completion: @escaping (SCNNode?) -> Void) {
    // Check that assets for that model are not already downloaded
    let fileManager = FileManager.default
    let dirForModel = modelsDirectory.appendingPathComponent(id)
    let dirExists = fileManager.fileExists(atPath: dirForModel.path)
    if dirExists {
        completion(loadNodeWithIdFromDisk(id))
    } else {
        let dumbURL = "http://yourserver/yourfile.zip"
        downloadZip(from: dumbURL, at: id) {
            if let url = [=10=] {
                print("Downloaded and unzipped at: \(url.absoluteString)")
                completion(self.loadNodeWithIdFromDisk(id))
            } else {
                print("Something went wrong!")
                completion(nil)
            }
        }
    }
}

func loadNodeWithIdFromDisk(_ id: String) -> SCNNode? {
    let fileManager = FileManager.default
    let dirForModel = modelsDirectory.appendingPathComponent(id) 
    do {
        let files = try fileManager.contentsOfDirectory(atPath: dirForModel.path)
        if let objFile = files.first(where: { [=10=].hasSuffix(".obj") }) {
            let objScene = try? SCNScene(url: dirForModel.appendingPathComponent(objFile), options: nil)
            let objNode = objScene?.rootNode.firstChild()
            return objNode
        } else {
            print("No obj file in directory: \(dirForModel.path)")
            return nil
        }
    } catch {
        print("Could not enumarate files or load scene: \(error)")
        return nil
    }
}

func downloadZip(from urlString: String, at destFileName: String, completion: ((URL?) -> Void)?) {
    print("Downloading \(urlString)")
    let fullDestName = destFileName + ".zip"

    let destination: DownloadRequest.DownloadFileDestination = { _, _ in
        let fileURL = modelsDirectory.appendingPathComponent(fullDestName)
        return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
    }

    Alamofire.download(urlString, to: destination).response { response in
        let error = response.error
        if error == nil {
            if let filePath = response.destinationURL?.path {
                let nStr = NSString(string: filePath)
                let id = NSString(string: nStr.lastPathComponent).deletingPathExtension
                print(response)
                print("file downloaded at: \(filePath)")
                let fileManager = FileManager()
                let sourceURL = URL(fileURLWithPath: filePath)
                var destinationURL = modelsDirectory
                destinationURL.appendPathComponent(id)
                do {
                    try fileManager.createDirectory(at: destinationURL, withIntermediateDirectories: true, attributes: nil)
                    try fileManager.unzipItem(at: sourceURL, to: destinationURL)
                    completion?(destinationURL)
                } catch {
                    completion?(nil)
                    print("Extraction of ZIP archive failed with error: \(error)")
                }
            } else {
                completion?(nil)
                print("File path not found")
            }
        } else {
            // Handle error
            completion?(nil)
        }
    }
}