Skip to content

Instantly share code, notes, and snippets.

@lukas2
Created January 5, 2018 10:56
Show Gist options
  • Save lukas2/9723e3b125e5eaf856848c5c015adca5 to your computer and use it in GitHub Desktop.
Save lukas2/9723e3b125e5eaf856848c5c015adca5 to your computer and use it in GitHub Desktop.
How to apply CI Filter in Swift
// example with CIColorPosterize
let context = CIContext(options: nil)
var ciImage = CIImage(cgImage: image.cgImage!)
ciImage = ciImage.applyingFilter("CIColorPosterize", parameters: ["inputImage" : ciImage, "inputLevels" : 2])
let cgImage = context.createCGImage(ciImage, from: ciImage.extent)!
let filteredUiImage = UIImage(cgImage: cgImage, scale: image.scale, orientation: .up)
@SamaritanMachine
Copy link

Hi, I've tried this for CIMaskToAlpha in my generating QR Code functio, but it didn't work. Do u have any solution?

func generateQRCode(from string: String) -> UIImage? {
        let data = string.data(using: String.Encoding.ascii)
        
        if let filter = CIFilter(name: "CIQRCodeGenerator") {
            filter.setValue(data, forKey: "inputMessage")
            let transform = CGAffineTransform(scaleX: 5, y: 5)
            
            if let output = filter.outputImage?.transformed(by: transform) {
                output.applyingFilter("CIMaskToAlpha", parameters: ["inputImage":output])
                return UIImage(ciImage: output)
                
            }
        }
        
        return nil
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment