gpt4 book ai didi

ios - 耗时操作前 View 不出现

转载 作者:行者123 更新时间:2023-11-28 06:36:49 26 4
gpt4 key购买 nike

我已经在我的应用程序中实现了这个指标:https://github.com/vincechan/SwiftLoadingIndicator

在这个加载旋转器出现在前面之后,我希望它一直旋转,只要我的操作持续。

在我的操作中,我对图像做了一些处理,然后将它们显示给用户,但每次我单击“运行操作”按钮后,应用程序都会卡住几秒钟,用结果更新 View ,然后显示覆盖微调器.我试过这样的异步调度:

 @IBAction func manipulateImage(sender: AnyObject) {
dispatch_async(dispatch_get_main_queue(), {
LoadingOverlayView.show()
});
if let beginImage = CIImage(image: self.imageView.image!) {
var outputImage = OutputImage(sourceImage: beginImage)
//apply CIFilters:
outputImage.applyFilter(FilterType.Grayscale)
outputImage.applyFilter(FilterType.Sepia)
outputImage.applyFilter(FilterType.Vignette)
outputImage.applyFilter(FilterType.Shadow)

let cgimg = self.imageContext.createCGImage(outputImage, fromRect: outputImage.extent)

self.imageView.image = UIImage(CGImage: cgimg)
LoadingOverlayView.hide()
}

但它不起作用。没有异步分派(dispatch)的普通方法调用的行为也完全相同。

最佳答案

您可能在 UI 线程上运行此代码。如果是这样,您可以按如下方式将操作分派(dispatch)到后台队列:

// we're already on the UI thread, so dispatch to a background queue
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// do your background operations ...
if let beginImage = CIImage(image: self.imageView.image!) {
var outputImage = OutputImage(sourceImage: beginImage)
//apply CIFilters:
outputImage.applyFilter(FilterType.Grayscale)
outputImage.applyFilter(FilterType.Sepia)
outputImage.applyFilter(FilterType.Vignette)
outputImage.applyFilter(FilterType.Shadow)

let cgimg = self.imageContext.createCGImage(outputImage, fromRect: outputImage.extent)

// make sure to dispatch UI function back to main queue!
dispatch_async(dispatch_get_main_queue(), {
self.imageView.image = UIImage(CGImage: cgimg)

LoadingOverlayView.hide()
});
}
});

// we're already on the UI thread, so show the overlay now
LoadingOverlayView.show()

关于ios - 耗时操作前 View 不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38903118/

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