title: How to center a CGImage on a CALayer?
body: <p>I was trying to set a <code>UIImage</code>'s <code>CGImage</code> as a <code>layer</code>'s content and then add the <code>layer</code> to a <code>view</code>'s <code>layer</code>.<br>
  It's should be five stars at the center of the yellow view. That's what I want it to be.<br>
  But it seems the center of the stars is aligned with the <code>origin</code> of the <code>view</code>.
  What should I do to rectify it?</p>
  
  <pre><code>func putOnStars() {
      let rect = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
      rect.backgroundColor = .yellow
      view.addSubview(rect)
  
      let baseLayer = CALayer()
      baseLayer.contents = UIImage(named: "stars")?.cgImage
      baseLayer.contentsGravity = kCAGravityCenter
      rect.layer.addSublayer(baseLayer)
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/8AL0D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8AL0D.png" alt="enter image description here"></a>
  Here is the stars image for you in case of you want to test.
  <a href="https://i.stack.imgur.com/WjOfU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WjOfU.png" alt="enter image description here"></a></p>