gpt4 book ai didi

ios - 当通过 contentOfURL 同步下载图像时开始动画 UIActivityIndi​​cator

转载 作者:行者123 更新时间:2023-11-29 00:50:35 25 4
gpt4 key购买 nike

我正在尝试在图像开始下载时显示 UIActivityIndi​​cator,并且它会显示动画,直到所有图像都下载完毕。但是 UIActivityIndi​​cator 不是动画,它是在所有图像都下载完后才动画。我发现 contentsOfURL 是同步方法并阻塞主线程。我想使用 contentsOfURL 下载图片。任何人都知道如何在图像开始下载时为 UIActivityIndi​​cator 设置动画?谢谢

var indeX = 0  
var imageArrayNsData : [NSData] = []
var posts = NSMutableArray()

myActivityIndicator.startAnimating()
self.view.addSubview(myActivityIndicator)
self.view.userInteractionEnabled = false

for _ in posts.valueForKey("enclosure") as! [NSString]{
let picURL = self.posts.objectAtIndex(indeX).valueForKey("enclosure") as! String
let url = NSURL(string: picURL)
let data = NSData(contentsOfURL: url!)
let IMG = UIImage(data: data!)
let dtA : NSData = NSData(data: UIImagePNGRepresentation(IMG!)!)
print("download")
imageArrayNsData.append(dtA)
indeX++
print(indeX)
}
self.view.userInteractionEnabled = true
myActivityIndicator.stopAnimating()

最佳答案

要异步执行此操作,您必须使用 dispatch。事实上,AlamofireImage 也使用 dispatch 来处理它。

在我看来,您可以将代码分配给队列用户初始值,并在完成 myActivityIndi​​catorself.view.userInteractionEnabled 后更新主队列。/p>

dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.rawValue), 0)) { 
//your download code here

dispatch_async(dispatch_get_main_queue()) {
self.view.userInteractionEnabled = true
myActivityIndicator.stopAnimating()
}
}

关于ios - 当通过 contentOfURL 同步下载图像时开始动画 UIActivityIndi​​cator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38167511/

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