How to make a portion of an UIImageView transparen

2019-08-15 08:20发布

问题:

Here is a photo explanation to better illustrate what I mean:

I have two UIImageViews, with View1 being on the bottom and View2 at the top:

What I would like to do accomplish is to programmatically set an area on View2 that is completely transparent (i.e. has an alpha of 0), so that this will be the end result:

I haven't been able to find a similar problem related to marking a part of a UIImageView transparent in the form of a shape (specifically, a circle), and was wondering how I should tackle this problem?

Thanks!

回答1:

One road you could go is CoreGraphics:

  1. Create an image context
  2. Set the clipping path you need (or just clear the circle for the "hole" after drawing)
  3. Draw the original image into the context
  4. Make an UIImage from that context
  5. Assign the image to the top UIImageView

https://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/HandlingImages/Images.html#//apple_ref/doc/uid/TP40010156-CH13-SW1