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

问题描述 投票:1回答:1

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

AF.download("http://ipv4.download.thinkbroadband.com/200MB.zip",
                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)
                    }
            }

使用通知发布进度

func postProgress(progress: Progress) {
        NotificationCenter.default.post(name: .DownloadProgress, object: progress)
    }

使用以下代码显示我的进度

if let progress = notification.object as? Progress {
            progressBar.setProgress(Float(progress.fractionCompleted), animated: true)
            progressInNumberView.text = "\(Int64(progress.fractionCompleted*100))%"
            remianingView.text = "\(progress.fileCompletedCount) / \(progress.fileTotalCount)"
            if let estimatedTimeRemaining = progress.estimatedTimeRemaining {

                remianingView.text = format(estimatedTimeRemaining)
            }
        }

我正在进步.fraction完美完成。但progress.estimatedTimeRemaining,progress.fileCompletedCount和progress.fileTotalCount始终为nil。

ios swift alamofire
1个回答
0
投票

我查看你能找到的Progress课程

open var totalUnitCount: Int64
open var completedUnitCount: Int64

你可以使用这些变量

© www.soinside.com 2019 - 2024. All rights reserved.