链接UIProgressView以使用Firebase显示下载进度

时间:2017-01-16 19:08:01

标签: ios firebase firebase-storage uiprogressview

我正在尝试显示我下载的文件的进度视图,当我打印进度时它是正确的但是当我在进度视图上实现它是完全错误的它是3000%这里是我的代码。我正在使用MHRadialProgressView

downloadTask.observe(.progress) { (snapshot) -> Void in
    // Download reported progress

    let percentComplete = 100 * Double(snapshot.progress!.completedUnitCount) / Double(snapshot.progress!.totalUnitCount)
    print("percentComplete")
    self.progressView.moveNext(currentprogress as NSNumber!)

    // Update the progress indicator
}

当我使用MBProgressHUD时,这里是代码

  downloadTask.observe(.progress) { (snapshot) -> Void in
                // Download reported progress    
                let percentComplete = 100 * Float(snapshot.progress!.completedUnitCount)
                    / Float(snapshot.progress!.totalUnitCount)

            let hud = MBProgressHUD.showAdded(to: (self.navigationController?.view)!, animated: true)
            // Set the determinate mode to show task progress.
            hud.mode = MBProgressHUDMode.determinate

            hud.label.text = NSLocalizedString("Loading...", comment: "HUD loading title")
            // Set up NSProgress
            let progressObject = Progress(totalUnitCount: 100)
            hud.progressObject = progressObject
            // Configure a cancel button.


            DispatchQueue.global(qos: .default).async(execute: {() -> Void in
                // Do something useful in the background and update the HUD periodically.

                while progressObject.fractionCompleted < 1.0 {

                    progressObject.becomeCurrent(withPendingUnitCount: 1)
                    progressObject.resignCurrent()
                    usleep(useconds_t(percentComplete))
                }

                DispatchQueue.main.async(execute: {() -> Void in
                    hud.hide(animated: true)
                })
            })
        }

我收到此错误 nanfatal错误:浮点值无法转换为UInt32,因为它是无限或NaN

1 个答案:

答案 0 :(得分:1)

一些建议:

  • 请勿使用此库(自2014年以来尚未更新),请使用MBProgressHUD,这样您就可以set a progress object and forget about it
  • 如果您仍想使用原始库,请务必阅读文档 - 它说“或无序的进度[原文如此](通常通过与[原文如此]用户操作相关联的步骤),您可以通过以下方式增加进度:值”。这意味着您需要使用当前进度来区分先前的进度并自行递增或递减。