title: Downsized Image gets blurry after being copied to Pasteboard - Swift 3.0
body: <p>I am capturing an image which is then placed in a small imageView. The picture is not blurry in the small imageView, but when I copy it to the clipboard, I am resizing the picture so that it is the same size as the imageView, but now it is blurry when I paste. </p>
  
  <p>Here is the code: </p>
  
  <pre><code>import UIKit
  import AVFoundation
  
  class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
      @IBOutlet weak var cameraView: UIView!
      @IBOutlet weak var imageView: UIImageView!
  
      var session: AVCaptureSession?
      var stillImageOutput: AVCaptureStillImageOutput?
      var videoPreviewLayer: AVCaptureVideoPreviewLayer?
      var captureDevice:AVCaptureDevice?
      var imagePicker: UIImagePickerController!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          alignment()
          tapToCopy()
      }
  
      override func viewWillAppear(_ animated: Bool) {
          session = AVCaptureSession()
          session!.sessionPreset = AVCaptureSessionPresetPhoto
  
          let videoDevices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo)
  
          for device in videoDevices!{
              let device = device as! AVCaptureDevice
              if device.position == AVCaptureDevicePosition.front {
                  captureDevice = device
              }
          }
  
          //We will make a new AVCaptureDeviceInput and attempt to associate it with our backCamera input device.
          //There is a chance that the input device might not be available, so we will set up a try catch to handle any potential errors we might encounter.
          var error: NSError?
          var input: AVCaptureDeviceInput!
          do {
              input = try AVCaptureDeviceInput(device: captureDevice)
          } catch let error1 as NSError {
              error = error1
              input = nil
              print(error!.localizedDescription)
          }
  
          if error == nil &amp;&amp; session!.canAddInput(input) {
              session!.addInput(input)
              // ...
              // The remainder of the session setup will go here...
  
              stillImageOutput = AVCaptureStillImageOutput()
              stillImageOutput?.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
  
              if session!.canAddOutput(stillImageOutput) {
                  session!.addOutput(stillImageOutput)
                  // ...
                  // Configure the Live Preview here...
  
                  videoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
                  videoPreviewLayer!.videoGravity = AVLayerVideoGravityResizeAspect
                  videoPreviewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
                  cameraView.layer.addSublayer(videoPreviewLayer!)
                  session!.startRunning()
              }
          }
      }
  
      func alignment() {
          let height = view.bounds.size.height
          let width = view.bounds.size.width
  
          cameraView.bounds.size.height = height / 10
          cameraView.bounds.size.width = height / 10
          cameraView.layer.cornerRadius = height / 20
  
          imageView.bounds.size.height = height / 10
          imageView.bounds.size.width = height / 10
          imageView.layer.cornerRadius = height / 20
          imageView.clipsToBounds = true
      }
  
      override func viewDidAppear(_ animated: Bool) {
          super.viewDidAppear(animated)
          videoPreviewLayer!.frame = cameraView.bounds
      }
  
      @IBAction func takePic(_ sender: Any) {
          if (stillImageOutput!.connection(withMediaType: AVMediaTypeVideo)) != nil {
              let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo)
              // ...
              // Code for photo capture goes here...
              stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (sampleBuffer, error) -&gt; Void in
                  // ...
                  // Process the image data (sampleBuffer) here to get an image file we can put in our captureImageView
  
                  if sampleBuffer != nil {
                      let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                      let dataProvider = CGDataProvider(data: imageData as! CFData)
                      let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)
                      let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)
                      // ...
                      // Add the image to captureImageView here...
  
                      self.imageView.image =  self.resizeImage(image: image, newHeight: self.view.bounds.size.height / 10)
                  }
              })
          }
      }
  
      func tapToCopy() {
          let imageTap = UITapGestureRecognizer(target: self, action: #selector(self.copyToClipboard(recognizer:)))
          imageTap.numberOfTapsRequired = 1
          imageView.isUserInteractionEnabled = true
          imageView.addGestureRecognizer(imageTap)
      }
  
      func copyToClipboard(recognizer: UITapGestureRecognizer) {
          UIPasteboard.general.image = self.resizeImage(image: imageView.image!, newHeight: self.view.bounds.size.height / 10)
      }
  
      func resizeImage(image: UIImage, newHeight: CGFloat) -&gt; UIImage {
          let scale = newHeight / image.size.height
          let newWidth = image.size.width * scale
          UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
          image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
          let newImage = UIGraphicsGetImageFromCurrentImageContext()
          UIGraphicsEndImageContext()
  
          return newImage!
      }
  }
  </code></pre>