大家好(我想这是我的第一个问题:)),
我是 Swift 和 iOS 编程的新手。我正在进行图像过滤,并且我正在尝试使用 UIActivityIndicatorView
等待加载过滤图像之前的时间。事件指示器包含在另一个带有文本标签的 View 中:“正在过滤”。但是等待 View 不会出现。
通过调试,我注意到执行一行代码后 View 不会立即更新; View 在整个 block 执行后更新。因此,在这种情况下,以下代码不起作用:
showActivityView() //shows the view with text and spinner
filteredImageView.image = imageProcessor.clearFilters().addFilter(currentFilter).filter() //shows filtered image on image view
hideActivityView() //hides the view with text and spinner
这不起作用,因为我猜,整个 View 在 hideActivityView() 完成后更新。关于如何以干净的方式实现它的任何想法。
如果有人知道的话,最好有一些好的相关引用资料。我正在努力寻找自己,但到目前为止还没有运气。 非常感谢。
最佳答案
您应该在另一个线程中进行过滤。您可以使用 Grand Central Dispatch ( tutorial here ) 来完成此操作。 您的代码将类似于以下内容:
showActivityView() //shows the view with text and spinner
var image: UIImage?
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), {
image = imageProcessor.clearFilters().addFilter(currentFilter).filter() //shows filtered image on image view
dispatch_async(dispatch_get_mein_queue(), {
filteredImageView.image = image!
hideActivityView() //hides the view with text and spinner
}
}
关于ios - 显示等待 View ,直到正在进行图像过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33912409/