ios - 显示等待 View ,直到正在进行图像过滤

标签 ios swift uiactivityindicatorview

大家好(我想这是我的第一个问题:)),

我是 Swift 和 iOS 编程的新手。我正在进行图像过滤,并且我正在尝试使用 UIActivityIndi​​catorView 等待加载过滤图像之前的时间。事件指示器包含在另一个带有文本标签的 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/

相关文章:

ios - 尝试访问分段控制索引时,Swift 抛出错误

c# - 在 Unity 中构建和加载 Assetbundle

ios - 隐藏的事件指示器无效

swift - 如何快速在 UITableView 上显示 UIActivityIndi​​cator

ios - 在类的每个函数中调用相同的代码

ios - Picker View 覆盖swift 4.2中模糊效果的值

ios - Swift 相当于 PackageInfo 和 ApplicationInfo

ios - UITableViewCell 没有扩展以适应 UILabel 的高度,我做错了什么?

ios - 将 UITextField 的输入添加到数组中

ios - 如何使用 Swift 以编程方式创建 UILabel?