What can be the solution of a deprecated of "EAGLContext"?

3.3k Views Asked by At

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

3

There are 3 best solutions below

1
matt On

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

0
jmrueda On

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.

0
Radu Ursache On

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