I'm having trouble combining two images together with CIFilter
. What is going wrong here?
The below code creates a UIImageView
and adds it to the view, then combines two images imageA
and imageB
with a CIFilter
, and outputs the composite into the UIImageView
.
However, the combined image is not displayed in the UIImageView
, it remains blank.
Questions:
- What is the correct code to display the composite image into the
UIImageView
?- Is there more performant way in which to combine two images with
CIFilter
?
Code:
let imageView = UIImageView()
imageView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
imageView.contentMode = .Center
view.addSubview(imageView)
let imageA = CIImage(image: UIImage(named:"imageA")!)
let imageB = CIImage(image: UIImage(named:"imageB")!)
let imageFilter = CIFilter(name: "CIAdditionCompositing")!
imageFilter.setValue(imageA, forKey: kCIInputImageKey)
imageFilter.setValue(imageB, forKey: kCIInputBackgroundImageKey)
if let imageCombined = imageFilter.outputImage {
let image = UIImage(CIImage: imageCombined)
imageView.image = image
}