title: Add a gradient on UIImageView
body: <p>I am trying to add a sublayer on my <code>UIImageView</code> but it doesn't work. </p>
  
  <ul>
  <li>I have a set of 10 images named from <code>photo0</code> to <code>photo9</code> and I display
  it with a timer of 5s.  </li>
  <li>The outlet <code>shanghaiImage</code> is my background </li>
  </ul>
  
  <p>I would like to add a gradient on top of this marty like: transparent (top) to black (bottom).</p>
  
  <p>Thanks for the help :) </p>
  
  <p>Here is my code in Swift 3. </p>
  
  <p>This part is fine : </p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
  @IBOutlet weak var shanghaiImage: UIImageView!
  
  // beginning index
  var _curentImageIndex:Int = 0
  // number of images
  let NUMBER_OF_IMAGES:Int = 10
  // creation of the Timer
  var _uiTimer:Timer?
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
      showPhoto(atIndex: _curentImageIndex)
  }
  
  // MARK TIMER ---------
  
  func selectNewTimer(){
      if let existingTimer:Timer = _uiTimer{
          existingTimer.invalidate()
      }
      _uiTimer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(ViewController.showNextImage), userInfo: nil, repeats: true)
  }
  </code></pre>
  
  <p>It's here where there is a problem. I don't know why it's not working.</p>
  
  <pre><code>// MARK PHOTO ---------
  func showPhoto(atIndex index:Int){
  
      let photoName:String =  "photo\(index)"
      shanghaiImage.image =  UIImage(named: photoName)
  
      let gradient = CAGradientLayer()
      gradient.frame = shanghaiImage.bounds
      let startColor = UIColor(colorLiteralRed: 0, green: 0, blue: 0, alpha: 1)
      let endColor = UIColor.black
  
      gradient.colors = [startColor, endColor]
      shanghaiImage.layer.insertSublayer(gradient, at: 0)
  
  
      _curentImageIndex  =  index
      selectNewTimer()
      }
  
  func showNextImage() {
      var nextPhotoIndex:Int = _curentImageIndex + 1
          if nextPhotoIndex &gt;= NUMBER_OF_IMAGES {
              nextPhotoIndex = 0
          }
      showPhoto(atIndex: nextPhotoIndex)
  }
  
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  }
  </code></pre>