我正在尝试在图像开始下载时显示 UIActivityIndicator
,并且它会显示动画,直到所有图像都下载完毕。
但是 UIActivityIndicator
不是动画,它是在所有图像都下载完后才动画。我发现 contentsOfURL
是同步方法并阻塞主线程。我想使用 contentsOfURL
下载图片。任何人都知道如何在图像开始下载时为 UIActivityIndicator
设置动画?
谢谢
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 来处理它。
在我看来,您可以将代码分配给队列用户初始值,并在完成 myActivityIndicator
和 self.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 同步下载图像时开始动画 UIActivityIndicator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38167511/