[英]AWS S3 download with iOS circular progress bar
我敢打赌这是一个非常基本的问题,但我无法解决这个问题。 我正在从AWS S3下载映像,能够获取下载进度,并能够相应地更新进度变量。 但是,我找不到基于此变量为进度条设置动画的方法。
(笨拙的)代码如下:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.refresh() //re-gets user info. Having trouble with AWS randomly claiming I'm attempting to download using UnAuth even though I am logged in
tableView.deselectRow(at: indexPath, animated: true)
self.downloadProgressView.isHidden = false
self.proprietaryObject = proprietaryObjects[indexPath.row]
let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in DispatchQueue.main.async(execute: {
self.progress = progress
})
}
let transferUtility = AWSS3TransferUtility.default()
let resource = "\(self.proprietaryObject?.value1 ?? "NA")_\(self.proprietaryObject?.value2 ?? "NA")_\(self.proprietaryObject?.firstName ?? "NA")_\(self.proprietaryObject?.lastName ?? "NA")"
let type = "jpeg"
self.downloadingFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(resource).\(type)")
transferUtility.download(to: self.downloadingFileURL!, bucket: AWSS3BucketName, key: "\(resource).\(type)", expression: expression, completionHandler: { (task, URL, Data, Error) in
if Error != nil {
print (Error!)
}
if let data = Data {
print("Data: \(data)")
let image = UIImage(data: data)
self.downloadedImage = image
}
}).continueWith(executor: AWSExecutor.default()) { (task) -> Any? in
if let error = task.error {
print("download error: \(error)")
self.refresh()
}else {
print("Download started")
print("User: \(self.user?.username ?? "NO USER")")
self.animatePath()
}
return nil
}
}
func animatePath() {
print("Animation Started")
if !(self.view.subviews.contains(downloadProgressView)) {
self.view.addSubview(downloadProgressView)
downloadProgressView.center = self.view.center
self.downloadProgressView.isHidden = false
self.downloadProgressView.layer.addSublayer(self.shapeLayer)
}else {
self.view.bringSubview(toFront: self.downloadProgressView)
self.downloadProgressView.isHidden = false
downloadProgressView.center = self.view.center
if !((downloadProgressView.layer.sublayers?.contains(self.shapeLayer))!) {
self.downloadProgressView.layer.addSublayer(self.shapeLayer)
}
}
func animate() {
if self.progress?.fractionCompleted == nil {
self.fraction = 0
}else {
self.fraction = 0 + (self.progress?.fractionCompleted)!
}
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = CGFloat(self.shapeLayer.strokeEnd)
animation.toValue = CGFloat(self.fraction!)
animation.duration = 0.2
animation.fillMode = kCAFillModeForwards
self.shapeLayer.strokeEnd = CGFloat(self.fraction!)
self.shapeLayer.add(animation, forKey: "animation")
}
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1) , execute: {
//the one second delay gives self.progress.fractionCompleted time to be non-nil
repeat {
animate()
print("Completed: \((self.progress?.fractionCompleted)! * 100)%")
} while self.fraction! < 1
if self.fraction == 1 {
print("fraction achieved 100%. resetting to 0%")
self.fraction = 0
self.progress = nil
self.performSegue(withIdentifier: "proprietarySegue", sender: self)
}
})
}
控制台记录增量“ .fractionCompleted”%很好,当达到100%时,它会执行segue。 以下ViewController的UIImageView中的图像确实是下载的图像(未显示重写准备工作)。 但是我只看到进度圈的一刹那。 通常,它显示没有进度,然后执行segue。 第一次搜索之后,紧随其后的每个下载,都不会出现downloadProgressView或在消失之前显示100%完成。
我感觉我在做一些根本错误的事情,并且/或者与线程有关...?
任何帮助,将不胜感激。
在transferUtility
完成块中调用了self.animatePath()
。 这就是为什么动画将在下载结束时发生并执行segue的原因。
尝试:
let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in
DispatchQueue.main.async(execute: {
self.progress = progress
self.animatePath()
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.