我正在 iOS 中开发一个应用程序,其中我需要开始旋转 UIActivityIndicatorView,将图像上传到服务器,并在上传完成后停止旋转事件指示器。
我目前正在使用 XCode 7 Beta,并在 iOS 模拟器上作为 iPhone 6 和 iPhone 5 测试该应用程序。我的问题是,事件指示器不会在文件上传后立即结束,而是会持续几个(约 28 秒) ) 之后。我应该在哪里调用电话才能结束?
我有一个 @IBOutlet 函数附加到用于启动进程的按钮,该函数包含 startAnimating() 函数,并调用一个包含对 uploadImage 的调用的dispatch_async 方法,该方法包含信号、等待和 stopAnimating () 函数。
请注意
let semaphore = dispatch_semaphore_create(0)
let priority = DISPATCH_QUEUE_PRIORITY_HIGH
是在我的类的顶部定义的。
@IBAction func uploadButton(sender: AnyObject) {
self.activityIndicatorView.startAnimating()
dispatch_async(dispatch_get_global_queue(priority, 0)) {
self.uploadImage(self.myImageView.image!)
} // end dispatch_async
} // works with startAnimating() and stopAnimating() in async but not with uploadImage() in async
func uploadImage(image: UIImage) {
let request = self.createRequest(image)
let session : NSURLSession = NSURLSession.sharedSession()
let task : NSURLSessionTask = session.dataTaskWithRequest(request, completionHandler: {
(data, response, error) in
if error != nil {
print(error!.description)
} else {
let httpResponse: NSHTTPURLResponse = response as! NSHTTPURLResponse
if httpResponse.statusCode != 200 {
print(httpResponse.description)
} else {
print("Success! Status code == 200.")
dispatch_semaphore_signal(self.semaphore)
}
}
})! // end dataTaskWithResult
task.resume()
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)
self.activityIndicatorView.stopAnimating()
} // end uploadImage
这只是我的代码的一个版本,我已经通过几种不同的方式移动了一些内容。我已经尝试过:
@IBAction func uploadButton(sender: AnyObject) {
self.activityIndicatorView.startAnimating()
dispatch_async(dispatch_get_global_queue(priority, 0)) {
self.uploadImage(self.myImageView.image!)
dispatch_semaphore_signal(self.semaphore)
} // end dispatch_async
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)
self.activityIndicatorView.stopAnimating()
}
还有几种其他方法来移动我的代码,以尝试让事件指示器在图像上传期间显示,然后立即退出。在某些情况下,旋转器在程序执行期间根本不会出现。我读过this发布和this问题并已将我的dispatch_semaphore_wait和stopAnimating()迁移到uploadImage()方法来规避这个问题,但在UIActivityIndicatorView文档中找不到有关UI更新的足够信息来了解更新它的任何其他方式,尽管我相信这可能是问题的核心。
我所需要的只是让微调器在上传过程开始(dataTaskWithRequest)之前启动,并在成功或失败后结束。我做错了什么?
最佳答案
您可以直接分派(dispatch)到异步任务中的主线程,而不是使用信号量,
func uploadImage(image: UIImage) {
let request = self.createRequest(image)
let session : NSURLSession = NSURLSession.sharedSession()
let task : NSURLSessionTask = session.dataTaskWithRequest(request, completionHandler: {
(data, response, error) in
if error != nil {
print(error!.description)
} else {
let httpResponse: NSHTTPURLResponse = response as! NSHTTPURLResponse
if httpResponse.statusCode != 200 {
print(httpResponse.description)
} else {
print("Success! Status code == 200.")
}
}
// dispatch to main thread to stop activity indicator
dispatch_async(disptach_get_main_queue()) {
self.activityIndicatorView.stopAnimating()
}
})! // end dataTaskWithResult
task.resume()
} // end uploadImage
关于ios - UIActivityIndicatorView 在异步任务期间不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32013372/