1

I have this UIImageView:

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

enter image description here

Now I am trying to add a coloured layer with an alpha of 0.3 on this image. I tried this:

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)

But I'm getting this result.

enter image description here

I understand that of course it's because I'm setting an overlay on the whole view. Is there any way so I can set the overlay only on the non-transparent part of the image ?

Thanks for your help.

Rob
  • 4,123
  • 3
  • 33
  • 53

1 Answers1

1

You need to play with blending modes for that.

Apple documentation : link

Some relevant stuff which might help : link1 link2

Hooda
  • 1,157
  • 1
  • 9
  • 16
  • Thank you for your reply! Got it working with this solution: https://stackoverflow.com/a/42353385/2885727 – Rob Aug 18 '17 at 20:50