在我的ViewController.viewWillAppear
中:
super.viewWillAppear(animated) var image = UIImage(named: "colorful")! let imageView = UIImageView(image: image) imageView.frame.origin.x = 67.0 imageView.frame.origin.y = 324.0 - image.size.height - 20.0 view.addSubview(imageView) var image2 = ImagePainter.paint(image: image, from: UIColor(red: 243.0/255.0, green: 209.0/255.0, blue: 0.0/255.0, alpha: 1.0), to: UIColor.blue)! let image2View = UIImageView(image: image2) image2View.frame.origin.x = 67.0 image2View.frame.origin.y = 324.0 view.addSubview(image2View)
}
ImagePainter:
import UIKit
import CoreImage
class ImagePainter {
static let context = CIContext()
static func paint(image: UIImage?, from: UIColor, to: UIColor) -> UIImage? {
guard let image = image,
let inputImage = CIImage(image: image) else {
return nil
}
let initFilter = CIFilter(name: "CIFalseColor")
initFilter?.setValue(inputImage, forKey: "inputImage")
initFilter?.setValue(CIColor(color: from), forKey: "inputColor1")
initFilter?.setValue(CIColor(color: to), forKey: "inputColor0")
guard let filter = initFilter,
let filterOutput = filter.outputImage else {
return nil
}
let cgimg = context.createCGImage(filterOutput, from: filterOutput.extent)
let processed = UIImage(cgImage: cgimg!, scale: UIScreen.main.scale, orientation: .up);
return processed
}
}
CIFalseColor
之前/之后:
此屏幕截图显示了原始图像(蓝色中的“来自”颜色)以及调用CIFalseColor的结果。没有任何色彩像我预期的那样。显然我做错了什么,但是呢?