Please tell me how to properly handle images in Core Image. The higher the resolution of the image, the longer it will take to wait for its processing. There is also a UISlider, which changes the filter intensity, which causes the application to crash or hang. Everything works well only with small images, for example 600x600.
Sample code for the GLKView test class:
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) } } } ViewController code:
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 } }