直接看代码
以 下载百度图片为例
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
}
}
网友评论