【问题标题】:How to add an overlay layer to a UIImageView but not on the transparent part of the image?如何将覆盖层添加到 UIImageView 但不在图像的透明部分?
【发布时间】:2017-08-18 17:43:32
【问题描述】:

我有这个UIImageView

self.princessImageView.image = UIImage(named: "princess")!

现在我正在尝试在此图像上添加一个 alpha 为 0.3 的彩色图层。我试过这个:

let overlayView: UIView = UIView(frame: CGRect(x: 0,
                                               y: 0,
                                               width: self.princessImageView.frame.size.width,
                                               height: self.princessImageView.frame.size.height))
overlayView.backgroundColor = UIColor.blue.withAlphaComponent(0.3)
self.princessImageView.addSubview(overlayView)

但我得到了这个结果。

我明白这当然是因为我在整个视图上设置了叠加层。有什么办法可以让我只在图像的非透明部分设置叠加层?

感谢您的帮助。

【问题讨论】:

    标签: ios uiimageview layer


    【解决方案1】:

    您需要为此使用混合模式。

    苹果文档:link

    一些可能有帮助的相关内容:link1 link2

    【讨论】:

    猜你喜欢
    • 2018-05-16
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2023-04-10
    • 2016-02-05
    相关资源
    最近更新 更多