gpt4 book ai didi

ios - 带有 iOS 圆形进度条的 AWS S3 下载

转载 作者:行者123 更新时间:2023-11-30 11:54:04 25 4
gpt4 key购买 nike

我敢打赌这是一个非常基本的问题,但我无法让它发挥作用。我正在从 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% 时,它会执行转场。下面的ViewController的UIImageView中的图像确实是下载的图像(覆盖prepare for segue未显示)。但我只看到进度圈的闪烁。通常,它不会显示任何进度,然后执行 segue。在第一个 segue 之后,对于每个后续下载,downloadProgressView 不会出现,或者在消失之前显示 100% 完成。

我有一种感觉,我正在做一些根本错误的事情和/或者它与线程有关......?

如有任何帮助,我们将不胜感激。

最佳答案

self.animatePath() 已在 transferUtility 的完成 block 中调用。这就是为什么动画会在下载结束时发生并执行segue。

尝试:

let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in
DispatchQueue.main.async(execute: {
self.progress = progress
self.animatePath()
})
}

关于ios - 带有 iOS 圆形进度条的 AWS S3 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48029597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com