title: How to copy an UIImage?
body: <p>I have an <code>UIImageView</code> with an <code>UIImage</code>. I want to assign a copy of these picture to two variables. Based on what the user is doing the image should be manipulated. The problem is, the image is the same in each variables. I guess, because they are passed by reference.</p>
  <pre><code>let image1 = imageView.image!
  let image2 = imageView.image!
  </code></pre>
  <p>How do I get two separate copies of this image?</p>
  <p>What I want to achieve: Just crop the one image, keep the other like the original.</p>
  <pre><code>let imageLeft = googleImageView.image!
  let imageRef = CGImageCreateCopy(googleImageView.image!.CGImage)
  let imageRight = UIImage(CGImage: imageRef!, scale: googleImageView.image!.scale, orientation: googleImageView.image!.imageOrientation)
  
  if translation.x &lt; 0 {
      let scale = imageLeft.scale
      let newRect = CGRectMake(0, 0, (imageLeft.size.width + translation.x) * scale, imageLeft.size.height * scale)
      let imageRef = CGImageCreateWithImageInRect(imageLeft.CGImage, newRect)
      
      if let croppedImage = imageRef {
          googleImageView.image = UIImage(CGImage: croppedImage, scale: scale, orientation: imageLeft.imageOrientation)
      }
  } 
  print(&quot;left image: \(imageLeft) right image \(imageRight)&quot;)
  </code></pre>
  <p>The code above prints to the console:</p>
  <pre><code>left image: &lt;UIImage: 0x7fd020dca070&gt;, {111, 167} 
  right image &lt;UIImage: 0x7fd020dda430&gt;, {111, 167}
  left image: &lt;UIImage: 0x7fd020df9ba0&gt;, {110, 167} 
  right image &lt;UIImage: 0x7fd020d45670&gt;, {110, 167}
  </code></pre>
  <p>... and so on. So, BOTH images gets a new size. Only the left Image should get cropped.</p>