从SCNScene网址获取下载进度 [英] Get download progress from SCNScene url

查看:67
本文介绍了从SCNScene网址获取下载进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ARKit,无法从SCNScene网址获得下载进度.这是我的代码:

I'm working with ARKit, I have trouble getting download progress from SCNScene url. This is my code:

func downloadModel(hitTestResult: ARHitTestResult) {
    DispatchQueue.global().async {
        // create loading view
        // I WANT TO GET DOWNLOAD PROGRESS HERE
        let loading = UIAlertController(title: nil, message: "Please wait...\(downloadProgressValueHere)%", preferredStyle: .alert)

        let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        loadingIndicator.startAnimating();

        loading.view.addSubview(loadingIndicator)
        self.present(loading, animated: true, completion: nil)

        // download 3d model from server
        let myURL = URL(string: "http://www.mydownloadlink.com")
        let scene = try! SCNScene(url: myURL!, options: nil)
        let node = (scene.rootNode.childNode(withName: "parentNode", recursively: true))!
        self.sceneView.scene.rootNode.addChildNode(node)

        // dismiss loading view
        loading.dismiss(animated: true, completion: nil)
    }
}

如何获得请稍候..."消息的下载进度?谢谢大家.

How can I get download progress to "Please wait..." message? Thanks all.

推荐答案

据我了解,尽管看起来以下方法实际上将采用远程URL,但实际上并没有(尽管我可能是错的):

It is my understanding that whilst it appears that the following method would take a remote URL in actuality it doesn't (although I could be wrong):

convenience init(url: URL, 
         options: [SCNSceneSource.LoadingOption : Any]? = nil) throws

无论如何在这里回答您的问题都是一个示例(可能有点OTT,但希望它也会对其他人有所帮助).

Anyway to answer your question here is an example for you (which is probably a bit OTT, but hopefully it will help others as well).

让我们说我们在以下URL处有一个SCNScene文件:

Lets say we an SCNScene file at the following URL:

http://stackOverflow.com/stackOverFlow.scn

首先,我们需要像这样为ProgressInfo创建变量:

Firstly we need to create variables for our ProgressInfo like so:

 var loading: UIAlertController!
 var loadingIndicator: UIActivityIndicatorView!
 var downloadString: String = "Downloading"

然后,我们将创建一个URLSession来下载文件,如下所示:

Then what we will do is create a URLSession to download the file like so:

 /// Downloads An SCNFile From A Remote URL
 func downloadSceneTask(){

        //1. Get The URL Of The SCN File
        guard let url = URL(string: "http://stackOverflow.com/stackOverFlow.scn") else { return }

        //2. Create The Download Session
        let downloadSession = URLSession(configuration: URLSession.shared.configuration, delegate: self, delegateQueue: nil)

        //3. Create The Download Task & Run It
        let downloadTask = downloadSession.downloadTask(with: url)
        downloadTask.resume()

        //4. Show The Progress Alert
        DispatchQueue.main.async {

           self.loading = UIAlertController(title: nil, message: self.downloadString , preferredStyle: .alert)

           let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
           loadingIndicator.hidesWhenStopped = true
           loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
           loadingIndicator.startAnimating();

           self.loading.view.addSubview(loadingIndicator)
           self.present(self.loading, animated: true, completion: nil)
    }

 }

请注意,我已经创建了一个variable引起的downloadString,我们稍后将对其进行更改.

Note that I have create a variable caused downloadString which we change later.

然后,我们将参考以下URLSession委托:

We will then make reference to following URLSession Delegates e.g:

class ViewController: UIViewController, URLSessionDelegate, URLSessionDownloadDelegate {

}

我们的示例将调用以下函数:

Which for our example will call the following functions:

第一个跟踪我们下载进度的信息,您可以在其中处理诸如在HUD或使用progressIndicator中显示进度的事情:

The 1st to track the progress of our download in which you can handle things like showing the progress in a HUD or using a progressIndicator:

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask,
                    didWriteData bytesWritten: Int64,
                    totalBytesWritten: Int64,
                    totalBytesExpectedToWrite: Int64) {

        print("Downloaded \(totalBytesWritten) / Of \(totalBytesExpectedToWrite) Bytes")

        DispatchQueue.main.async {
           self.loading.message = "Downloaded \(totalBytesWritten) / Of \(totalBytesExpectedToWrite) Bytes"
    }

}

文件下载后处理的第二个:

The 2nd to handle when the file has downloaded:

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {

//1. Remove The Loading View
    loading.dismiss(animated: true, completion: nil)

//2. Create The Filename
let fileURL = getDocumentsDirectory().appendingPathComponent("stackOverFlow.scn")

//3. Copy It To The Documents Directory
do {
    try FileManager.default.copyItem(at: location, to: fileURL)

    print("Successfuly Saved File \(fileURL)")

    //4. Load The Model
    loadModel()

} catch {

    print("Error Saving: \(error)")
 }
}

请注意,在这种方法中,我正在使用以下功能来检索下载的文件并将其复制到documents directory:

Note that in this method I am making use of the following function to retrieve and copy the downloaded file to the documents directory:

/// Returns The Documents Directory
///
/// - Returns: URL
func getDocumentsDirectory() -> URL {

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

}

一旦文件被下载并复制到整个文件中,我们(4)就会这样调用我们的loadModel function:

Once the file has been downloaded and copied across we then (4) call our loadModel function like so:

/// Loads The SCNFile From The Documents Directory
func loadModel(){

    //1. Get The Path Of The Downloaded File
    let downloadedScenePath = getDocumentsDirectory().appendingPathComponent("stackOverFlow.scn")

    do {

        //2. Load The Scene Remembering The Init Takes ONLY A Local URL
        let modelScene =  try SCNScene(url: downloadedScenePath, options: nil)

        //3. Create A Node To Hold All The Content
        let modelHolderNode = SCNNode()

        //4. Get All The Nodes From The SCNFile
        let nodeArray = modelScene.rootNode.childNodes

        //5. Add Them To The Holder Node
        for childNode in nodeArray {
            modelHolderNode.addChildNode(childNode as SCNNode)
        }

        //6. Set The Position
        modelHolderNode.position = SCNVector3(0, 0, -1.5)

        //7. Add It To The Scene
        self.augmentedRealityView?.scene.rootNode.addChildNode(modelHolderNode)


    } catch  {
        print("Error Loading Scene")
    }

}

请注意,这是一个非常粗糙的示例,并没有涉及检查文件是否存在,缩放等内容,但是它足以帮助您实现所需的内容...

Please note that this is a very crude example, and doesn't cover things like checking if the file exists, scaling etc, but it should be enough to help you achieve what you are looking for...

这篇关于从SCNScene网址获取下载进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆