Search code examples
swiftcore-imagecifilter

How to implement CICheckerboardGenerator in swift 4?


@IBOutlet var imageView: UIImageView!

var context: CIContext!
var currentFilter: CIFilter!


override func viewDidLoad() {

    super.viewDidLoad()

    context = CIContext()

    currentFilter = CIFilter(name: "CICheckerboardGenerator", parameters: ["inputColor0" : CIColor.white, "inputColor1" : CIColor.black, "inputCenter" : CIVector(x: 0, y: 0), "inputWidth" : 50.00])

    if let cgimg = context.createCGImage(currentFilter.outputImage!, from: currentFilter.outputImage!.extent) {
        let processedImage = UIImage(cgImage: cgimg)
        imageView.image = processedImage
    }
}

I have created two variables at the top of the class and in viewDidLoad() function trying to generate the checkerboard. What am I doing wrong? I know this filter does not require an input image. It does not create an image as I would expect it to do.


Solution

  • You need to create the CGImage as below,

    if let cgimg = context.createCGImage(currentFilter.outputImage!, from: self.imageView.bounds) {
        self.imageView.image = UIImage(cgImage: cgimg)
    }