What do I need for masking a UIImageView and how do I do it in Swift 3?
<p>I was wondering what I would need if I wanted to use a mask image to get my <code>UIImageView</code> in a specific shape. From what I understand, to create a mask, I need to have an image with the shape of the mask all black on top of a white background. Something like this, for example:</p>

<p><a href="https://i.stack.imgur.com/hU4lR.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/hU4lR.jpg" alt="Star-shaped mask image"></a></p>

<p>First of all, is this sufficient to shape an image view, and if so, how do I do it in Swift 3? I can only find masking code that is either outdated or written in Objective-C. I've tried simply assigning the image above to an <code>UIImageView</code> and then assign the image view to the <code>mask</code> property of the <code>UIImageView</code> I want to shape, like so:</p>

<pre><code>self.defaultImageView.mask = self.maskImageView
</code></pre>

<p>This didn't do anything. It just made <code>self.maskImageView</code> disappear (both image view's added through the storyboard and connected using <code>IBOutlet</code> properties). I'm sure I'm forgetting to do something. It can't be this simple. I would appreciate it if someone could help me out. Like I said, I put both image views on the exact same spot, on top of each other, in the storyboard.</p>

<p><strong>UPDATE:</strong>
My first attempt to set the mask programmatically after deleting it from my storyboard.</p>

<pre><code>let layer:CALayer = CALayer()
    let mask:UIImage = UIImage(named: "Black-Star-Photographic-Agency")!
    layer.contents = mask
    layer.frame = CGRect(x: 0, y: 0, width: ((self.defaultImageView.image?.size.width)!), height: (self.defaultImageView.image?.size.height)!)
    self.defaultImageView.layer.mask = layer
    self.defaultImageView.layer.masksToBounds = true
</code></pre>

<p>The result was that the image view had completely disappeared and wasn't visible anymore. Am I doing something, am I forgetting something or both?</p>