美文网首页
Alamofire5.2.2 下载文件带进度

Alamofire5.2.2 下载文件带进度

作者: 微笑中的你 | 来源:发表于2020-12-04 16:07 被阅读0次

    直接看代码

    以 下载百度图片为例

    
            let url: String = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607076334355&di=f21df7765a2baca90d61920261361190&imgtype=0&src=http%3A%2F%2Fattachments.gfan.com%2Fforum%2F201503%2F19%2F211608ztcq7higicydxhsy.jpg"
    
           //指定位置,且删除已存在的该文件
            let dest: DownloadRequest.Destination = { _, _ in
                let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
                let fileURL = documentsURL.appendingPathComponent("mm.png")
    
                return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
            }
    //开始下载
            AF.download(url, interceptor: nil, to: dest).downloadProgress(closure: { (progress) in
                
                debugPrint("-------下载进度: \(progress.fractionCompleted)")
            }).responseData { (res) in
                if let data = res.value {
                  //显示图片
                    let image = UIImage(data: data)
                    self.imgGif.image = image
                }
            }
    

    相关文章

      网友评论

          本文标题:Alamofire5.2.2 下载文件带进度

          本文链接:https://www.haomeiwen.com/subject/ehriwktx.html