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

    1 answer 1

    In the context of CI / CG, in my similar application I was able to optimize the implementation like this. With large photos starts to slow down, but not critical. It is better to test on the device, the simulator slows down. I initialized CIContext without OpenGL. Swift3:

     @IBAction func sliderMoved(_ sender: UISlider) { let context = CIContext(options: nil) let filter = CIFilter(name: "CIColorControls") let myQueue = DispatchQueue.global(qos: .userInteractive) myQueue.async { let ciImageLoaded = CIImage(cgImage: (self.imageLoaded?.cgImage)!) filter?.setValue(ciImageLoaded, forKey: kCIInputImageKey) filter?.setValue(NSNumber(value: sender.value as Float), forKey: kCIInputBrightnessKey) if let _output = filter?.outputImage, let _extent = filter?.outputImage?.extent { if let cgImage = context.createCGImage(_output, from: _extent) { DispatchQueue.main.async { self.finalImage = UIImage(cgImage: cgImage) self.imageView.image = self.finalImage } } } } } 
    • Alas, but it slows me down ((. I don’t even know what kind of magic Apple has in its native Photo app. - Metman
    • @Metman there are a lot of tricks, the whole section about this: ( developer.apple.com/library/content/documentation/… ) - Maxim Aba
    • Read the documentation. There are some tips on improving performance. But for some reason it does not help. I use GLKView for drawing, a little faster, but still slow. - Metman