菜鸟教程小白 发表于 2022-12-11 22:27:40

ios - 检索 Alamofire 估计剩余时间和文件下载大小


                                            <p><p>我正在尝试使用 Alamofire 下载文件。为了显示进度,我正在使用来自 alamofire 的 downloadProgress。我从中得到了进度百分比。但我需要显示到目前为止下载的总 Mb/Kb 以及估计的剩余时间。这是我的代码:</p>

<pre><code>AF.download(&#34;http://ipv4.download.thinkbroadband.com/200MB.zip&#34;,
                method: .get,
                parameters: nil,
                encoding: URLEncoding.default,
                headers: nil,
                interceptor:nil,
                to: destination)
                .downloadProgress { progress in
                  self.postProgress(progress: progress)
                }
                .responseData { response in
                  if let data = response.result.value {
//                        let image = UIImage(data: data)
                  }
            }
</code></pre>

<p>使用通知发布进度</p>

<pre><code>func postProgress(progress: Progress) {
      NotificationCenter.default.post(name: .DownloadProgress, object: progress)
    }
</code></pre>

<p>使用以下代码显示我的进度</p>

<pre><code>if let progress = notification.object as? Progress {
            progressBar.setProgress(Float(progress.fractionCompleted), animated: true)
            progressInNumberView.text = &#34;\(Int64(progress.fractionCompleted*100))%&#34;
            remianingView.text = &#34;\(progress.fileCompletedCount) / \(progress.fileTotalCount)&#34;
            if let estimatedTimeRemaining = progress.estimatedTimeRemaining {

                remianingView.text = format(estimatedTimeRemaining)
            }
      }
</code></pre>

<p>我得到了progress.fractionCompleted 完美。但progress.estimatedTimeRemaining、progress.fileCompletedCount 和progress.fileTotalCount 总是为零。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>简单的 DownloadProgress 类实现,基于来自 Alamofire 或其他的 <code>Progress</code> 以及从此处获取的移动平均值
<a href="https://stackoverflow.com/questions/2779600/how-to-estimate-download-time-remaining-accurately" rel="noreferrer noopener nofollow">How to estimate download time remaining (accurately)?</a> </p>
<p> <a href="https://gist.github.com/akovalov/7b22735e1fb7f1117bf04659e214d785" rel="noreferrer noopener nofollow">https://gist.github.com/akovalov/7b22735e1fb7f1117bf04659e214d785</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 检索 Alamofire 估计剩余时间和文件下载大小,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/55197497/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/55197497/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 检索 Alamofire 估计剩余时间和文件下载大小