我们如何降低 Swift 中 UIView 的饱和度?

How can we decrease saturation of UIView in Swift?

UITableView 有游戏关卡列表。我想通过降低 UITableViewCell 的饱和度来降低那些为用户锁定的记录的饱和度。我正在浏览 WWDC 2019 SwiftUI 教程,我看到它很容易在 SwiftUI.saturation 属性 的任何视图中完成,它会降低整个视图的饱和度,包括它的子视图如下:

Normal View

View with decreased saturation

有没有什么方法可以在 Swift 中以编程方式实现相同的目的?降低颜色的饱和度不是我想要的。

更新答案(仅用于饱和 - 堆栈溢出规则:一个问题,一个答案,而不是更多问题...)

extension UIImage {

    func withSaturationAdjustment(byVal: CGFloat) -> UIImage {
        guard let cgImage = self.cgImage else { return self }
        guard let filter = CIFilter(name: "CIColorControls") else { return self }
        filter.setValue(CIImage(cgImage: cgImage), forKey: kCIInputImageKey)
        filter.setValue(byVal, forKey: kCIInputSaturationKey)
        guard let result = filter.value(forKey: kCIOutputImageKey) as? CIImage else { return self }
        guard let newCgImage = CIContext(options: nil).createCGImage(result, from: result.extent) else { return self }
        return UIImage(cgImage: newCgImage, scale: UIScreen.main.scale, orientation: imageOrientation)
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let image = UIImage(named: "b")
        view.addSubview(UIImageView(image: image))

        let imageView2 = UIImageView(image: image?.withSaturationAdjustment(byVal: 0.3))
        imageView2.frame.origin.y = 300
        view.addSubview(imageView2)

    }


}