title: UIImage downsizing - loss of resolution
body: <p>I am building a custom image keyboard and am having trouble properly resizing an image. I have tried a bunch of different methods with no success. The issue is the image is either bigger than I would like it to be or, if I resize it to half of my intended size, then it is more blurry than the original is. In the following example I am attempting to resize the image to the same size (see bottom of post for a screenshot) as it is being displayed on screen. Here is my code for calling the resizing:</p>
  
  <pre><code>println("Starting Size is \(image!.size)")
  println("New Size is: \(sender.frame.size))")
  println("Initial Scale: \(image!.scale)")
  
  image = imageResize(image!, size: CGSize(width: sender.frame.width, height: sender.frame.height))
  
  println("Final Size is \(image!.size)")
  println("Final Scale: \(image!.scale)")
  </code></pre>
  
  <p>Here is the console output for the above print statements:</p>
  
  <pre><code>Starting Size is (750.0, 750.0)
  New Size is: (78.0, 78.0))
  Initial Scale: 1.0
  Final Size is (78.0, 78.0)
  Final Scale: 2.0
  </code></pre>
  
  <p>Here is the <code>imageResize</code> function</p>
  
  <pre><code>func imageResize(image:UIImage, size:CGSize)-&gt; UIImage {
      UIGraphicsBeginImageContextWithOptions(size, false, 0)
      var context = UIGraphicsGetCurrentContext()
  
      CGContextSetInterpolationQuality(context, kCGInterpolationHigh)
      image.drawInRect(CGRect(origin: CGPointZero, size: size))
  
      let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
  
      return scaledImage
  }
  </code></pre>
  
  <p>Finally, here is screenshot:</p>
  
  <p><a href="https://i.stack.imgur.com/FC99Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FC99Y.png" alt="Example Image Resizing"></a></p>
  
  <p><strong>Update:</strong></p>
  
  <p>To clarify, the image on the bottom of the screenshot is from the keyboard. It is a UIButton with the full size (750x750) image placed in it. When I am copying the image to the pasteboard, I begin with the full size image and downsize it to match the frame of the UIButton in the keyboard. So I am trying to get the image in the text message to be the same size and have the same clarity of the image in the keyboard.</p>
  
  <p><strong>Update #2</strong></p>
  
  <p>I updated my image resizing function to adjust the image size depending on the screen scale:</p>
  
  <pre><code>func imageResize(image:UIImage, size:CGSize)-&gt; UIImage {
  
      let scale  = UIScreen.mainScreen().scale
      let newSize = CGSize(width: size.width / scale, height: size.height / scale)
  
      UIGraphicsBeginImageContextWithOptions(newSize, false, scale)
      var context = UIGraphicsGetCurrentContext()
  
      CGContextSetInterpolationQuality(context, kCGInterpolationHigh)
      image.drawInRect(CGRect(origin: CGPointZero, size: newSize))
  
      let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
  
      return scaledImage
  }
  </code></pre>
  
  <p>Here is the console output will some details about the image:</p>
  
  <pre><code>Starting Size is (750.0, 750.0)
  New Size is: (78.0, 78.0))
  Initial Scale: 1.0
  Final Size is (39.0, 39.0)
  Final Scale: 2.0
  </code></pre>
  
  <p>Lastly, here is what the image looks like on the device (iPhone 6):</p>
  
  <p><a href="https://i.stack.imgur.com/Uzg4k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Uzg4k.png" alt="Second Screenshot"></a></p>
  
  <p>As you can see, although the image has the proper frame, it appears blurry in comparison to the UIButton in the keyboard. I should be able to achieve the same resolution as the UIButton since I am starting with the full size image in both cases. Any thoughts as to why I am losing resolution?</p>
  
  <p><strong>Update #3</strong></p>
  
  <p>I updated my code to multiply the image by the scale as opposed to dividing by it. </p>
  
  <pre><code>let newSize = CGSize(width: size.width * scale, height: size.height * scale)
  </code></pre>
  
  <p>Here is the result:</p>
  
  <pre><code>Starting Size is (750.0, 750.0)
  New Size is: (78.0, 78.0))
  Initial Scale: 1.0
  Final Size is (156.0, 156.0)
  Final Scale: 2.0
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/TkbCG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TkbCG.png" alt="Update 3"></a></p>
  
  <p>I really appreciate your help, but I am not still not understanding how to pull this all together to get a properly sized photo. To completely clarify my goal, I am trying to make the image copied into the message appear exactly like the image in the UIButton within the keyboard. Here is the code I am using to add the image to the pasteboard to copy it into the message text. The pasteboard code is run after the image is resized:</p>
  
  <pre><code>pb.setData(UIImagePNGRepresentation(image), forPasteboardType: type)
  </code></pre>