4

I want to use the native filters for my app, the function works but I want to avoid methods that will be removed from the documentation. I search over the whole internet and no solution.

I search over the whole internet and i haven't found any solution at my problem.

public func applyFilterTo(image: UIImage, filterEffect: Filter) -> UIImage? {
    guard let cgImage = image.cgImage,
          let openGLContext = EAGLContext(api: .openGLES3) else {
            return nil
    }
    let context = CIContext(eaglContext: openGLContext)
    let ciImage = CIImage(cgImage: cgImage)
    let filter = CIFilter(name: filterEffect.filterName)

    filter?.setValue(ciImage, forKey: kCIInputImageKey)

    if let filterEffectValue = filterEffect.filterEffectValue, let filterEffectValueName = filterEffect.filterEffectValueName {
        filter?.setValue(filterEffectValue, forKey: filterEffectValueName)
    }

    var filteredImage: UIImage?

    if let output = filter?.value(forKey: kCIOutputImageKey) as? CIImage,
        let cgiImageResult = context.createCGImage(output, from: output.extent) {
        filteredImage = UIImage(cgImage: cgiImageResult)
    }

    return filteredImage

}

The result is good but my concern is voiding warnings in my app. Thanks

Robby
  • 197
  • 1
  • 2
  • 14
  • The only mention here of EAGLContext being deprecated is https://stackoverflow.com/questions/51922595/confusion-about-cicontext-opengl-and-metal-swift-does-cicontext-use-cpu-or-g?r=SearchResults&s=2|22.9482 – rmaddy Apr 08 '19 at 22:29
  • @rmaddy What's your point? It's certainly deprecated. https://developer.apple.com/documentation/opengles/eaglcontext – matt Apr 08 '19 at 23:12
  • @matt My point is that the link mentions that it's deprecated and it seems to offer other options, maybe. – rmaddy Apr 08 '19 at 23:17
  • @rmaddy ok good answer! So should we close as duplicate? – matt Apr 08 '19 at 23:58

3 Answers3

2

EAGLContext is part of OpenGL, which is deprecated. You should switch to Metal at this stage.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • EAGLContext is **not** part of OpenGL. It is a platform-specific way of creating an OpenGL context. – lisyarus Nov 24 '20 at 13:59
0

Replace my filter with your custom settings to migrate away from OpenGL:

extension UIImage {
  func applyFilter() {
    guard let filter = CIFilter(name: "CIColorControls"), let coreImage = CIImage(image: self) else {
      return nil
    }
        
    let context = CIContext(options: nil)
        
    filter.setValue(coreImage, forKey: kCIInputImageKey)
    filter.setValue(7, forKey: kCIInputContrastKey)
    filter.setValue(1, forKey: kCIInputSaturationKey)
    filter.setValue(1.2, forKey: kCIInputBrightnessKey)

    guard let output = filter.outputImage, let cgImage = context.createCGImage(output, from: output.extent) else {
      return nil
    }
        
    return UIImage(cgImage: cgImage)
  }
}
Radu Ursache
  • 1,230
  • 1
  • 22
  • 35
-1

You do NOT strictly need to switch to Metal.

If you just want to avoid these warnings without but you don't want to switch to Metal because it could be an overkill (like my case) or a big time investment, you can always set the flags in the compiler to avoid these warnings.

If you pass the mouse over the warning itself it will let you know which is the specific flag to activate (i.e. CI_SILENCE_GL_DEPRECATION).

Then you just go to your Project File > Build Settings > Select 'All' and search for 'flags' > Apple Clang - Custom Compiler flags > Insert the Flag. Also check for Swift Compiler - Custom Flags.

Dharman
  • 30,962
  • 25
  • 85
  • 135
jmrueda
  • 1,362
  • 19
  • 30