title: Changing color of image embedded to a button (Swift3)
body: <p>I am able to change the color of an image of a <code>UIButton</code> from black to white with the following code:</p>
  
  <pre><code>extension UIImage {
      func maskWith(color: UIColor) -&gt; UIImage {
          UIGraphicsBeginImageContextWithOptions(size, false, scale)
  
          let context = UIGraphicsGetCurrentContext()!
          context.translateBy(x: 0, y: size.height)
          context.scaleBy(x: 1.0, y: -1.0)
          context.setBlendMode(.normal)
  
          let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
          context.clip(to: rect, mask: cgImage!)
  
          color.setFill()
          context.fill(rect)
  
          let newImage = UIGraphicsGetImageFromCurrentImageContext()!
  
          UIGraphicsEndImageContext()
  
      return newImage
      }
  }
  </code></pre>
  
  <p>I am setting the color of the image of the <code>UIButton</code> using the following:</p>
  
  <pre><code>//creditCardBtn is the the button variable
  creditCardBtn.imageView?.image? = (creditCardBtn.imageView?.image?.maskWith(color: .white))!
  </code></pre>
  
  <p><strong>My Issue</strong>
  When the user sets their finger on the button and then slowly drags their finger away, the image color resets itself. My thought was to use an <code>@IBAction</code> and reset the <code>UIButton</code>'s image when there is a <code>Touch Up Inside</code>. However this did not prevent the image from resetting its color. </p>
  
  <p>Here is the code I tried:</p>
  
  <pre><code>@IBAction func creditCardTap(_ sender: UIButton) {
      creditCardBtn.imageView?.image?.maskWith(color: .white)
  }
  </code></pre>
  
  <p><strong>What I am looking for:</strong>
  How to prevent the button from resetting its color from UI activities.</p>