'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 1:[1]
I believe the main issue in the initial question was the size of the resulting image: currentFilter.outputImage!.extent.
The image generated by CICheckerboardGenerator is basically an infinite plane (to the extent of available CGFloat values). In order to use the resulting CIImage you need to crop it:
if let pattern = currentFilter.outputImage {
let image = pattern.cropped(to: self.imageView.bounds)
self.imageView.image = UIImage(ciImage: image)
}
This way you don't need neither CIContext nor intermediate CGImage
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | badew |
