UITableViewCell 中的 Swift 模糊图像运行不流畅

问题描述

我想要做的是显示一个包含来自网络的模糊图像的列表。这适用于我的自定义 UITableViewCell 中的这段代码

func blurImage(image:UIImage,imageView: UIImageView) {

    dispatchQueue.global(qos: .background).async {
    
        let inputimage = CIImage(image: image)
        let originalOrientation = image.imageOrientation
        let originalScale = image.scale

          let filter = CIFilter(name: "CIGaussianBlur")
          filter?.setValue(inputimage,forKey: kCIInputimageKey)
          filter?.setValue(15.0,forKey: kCIInpuTradiusKey)
          let outputimage = filter?.outputimage

          var cgImage:CGImage?

          if let outputimage = outputimage{
            cgImage = self.context.createCGImage(outputimage,from: (inputimage?.extent)!)
          }
        
        dispatchQueue.main.async {
                if let cgImageA = cgImage{
                    imageView.image = UIImage(cgImage: cgImageA,scale: originalScale,orientation: originalOrientation)
                }
            }
        }
    }

问题是模糊计算需要一些时间,并且在 BG 线程上的所有想法滚动都没有那么快和平滑,好像我根本没有模糊效果

有没有办法让它运行更流畅或显示占位符图像,直到准备好再次绘制模糊图像以实现平滑滚动?

解决方法

步骤 1) 不要将 qos: .background 用于用户启动的任务。文档说:后台任务在所有任务中的优先级最低。当您的应用在后台运行时,将此类分配给您用来执行工作的任务或调度队列。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...