我用ViewController、GLKView和UISlider创建了测试应用程序。
滑块更改选定筛选器中的值。渲染图像非常慢。
我的密码怎么了?
GLKview的测试类:

import UIKit
import CoreImage
import GLKit

class CustomGLView: GLKView {
    //test filters
    let clampFilter = CIFilter(name: "CIAffineClamp")!
    let blurFilter = CIFilter(name: "CIGaussianBlur")!
    let ciContext:CIContext

    override init(frame: CGRect) {
        let glContext = EAGLContext(api: .openGLES2)
        ciContext = CIContext(
            eaglContext: glContext!,
            options: [
                kCIContextWorkingColorSpace: NSNull()
            ]
        )
        super.init(frame: frame, context: glContext!)
        enableSetNeedsDisplay = true
    }

    required init(coder aDecoder: NSCoder) {
        let glContext = EAGLContext(api: .openGLES2)
        ciContext = CIContext(
            eaglContext: glContext!,
            options: [
                kCIContextWorkingColorSpace: NSNull()
            ]
        )
        super.init(coder: aDecoder)!
        context = glContext!
        enableSetNeedsDisplay = true
    }

      var inputImage: UIImage? {
        didSet {
            inputCIImage = inputImage.map { CIImage(image: $0)! }
        }
    }

     var blurRadius: Float = 0 {
        didSet {
            blurFilter.setValue(blurRadius, forKey: "inputRadius")
            setNeedsDisplay()
        }
    }

    var inputCIImage: CIImage? {
        didSet { setNeedsDisplay() }
    }

    override func draw(_ rect: CGRect) {
        if let inputCIImage = inputCIImage {
            clampFilter.setValue(inputCIImage, forKey: kCIInputImageKey)
            blurFilter.setValue(clampFilter.outputImage!, forKey: kCIInputImageKey)
            let rect = CGRect(x: 0, y: 0, width: drawableWidth, height: drawableHeight)
            ciContext.draw(blurFilter.outputImage!, in: rect, from: inputCIImage.extent)
        }
    }
}

如何更改CIFilter中的值:
import UIKit
import GLKit
import CoreImage

    class ViewController: UIViewController {
        //image
         let imageOriginal = UIImage(named: "pic_2")
         //my GLKView
        @IBOutlet weak var glView: CustomGLView!

        override func viewDidLoad() {
                super.viewDidLoad()
                //test image
                self.glView.inputImage = self.imageOriginal

            }

        @IBAction func mySlider(_ sender: UISlider) {

                self.glView.blurRadius = sender.value
            }
    }

最佳答案

CIImage创建UIImage可能需要额外的时间将图像数据从内存复制到GPU内存。尝试将图像加载到GPU纹理中,然后使用CIImage创建由该纹理支持的CIImage(texture:size:flipped:colorSpace:)
确保只创建一次纹理,而不是在每个draw(_:)开始时。
您还可以尝试:
确保模糊半径不太大。高斯模糊是一个昂贵的操作,它将需要更长的模糊半径更大。
使用较小的图像。如果需要更大的图像,可能需要先缩小图像,应用模糊,然后再放大(有关说明,请参见here)。将宽度和高度减半将使像素数减少4倍。
UISlider将以每秒60的速率变化。将帧速率降低到30 fps将使渲染每个图像的时间增加一倍。

10-08 08:37
查看更多