如何将覆盖层添加到 UIImageView 但不在图像的透明部分?

How to add an overlay layer to a UIImageView but not on the transparent part of the image?

我有这个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)

但我得到了这个结果。

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

感谢您的帮助。

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

A​​pple 文档:link

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