我用 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
}
}