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

Posted

技术标签:

【中文标题】我们如何降低 Swift 中 UIView 的饱和度?【英文标题】:How can we decrease saturation of UIView in Swift? 【发布时间】:2020-05-27 10:25:17 【问题描述】:

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

普通视图

饱和度降低的视图

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

【问题讨论】:

【参考方案1】:

更新的答案(只是为了饱和 - stackoveflow 规则:一个问题,一个答案,而不是更多问题......)

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)

    



【讨论】:

我正在寻找降低整个视图(包括其子视图)的饱和度.. 您问了 2 个问题。第一:价值饱和。我已经回答过了。第二:整个视图-> 新问题。所以请阅读***.com/help/how-to-ask 是的..我的第一个问题已包含在您的回答中。回答赞成!

以上是关于我们如何降低 Swift 中 UIView 的饱和度?的主要内容,如果未能解决你的问题,请参考以下文章

iOS Swift 如何告诉 UIView 控制器我们通过 NSURLProtocol 对象接收到信息

在 .NET 中使用 GDI 调整图像大小会降低饱和度

如何在标签中打印uiview(swift3)

如何在 Swift 中使用 SnapKit 使 UIView 具有相等的宽度和高度?

R:降低调色板的色彩饱和度

如何使用 Swift 3 在 UIView 中覆盖 requiresConstraintBasedLayout?