title: Convert UIImage to grayscale keeping image quality
body: <p>I have this extension (found in <code>obj-c</code> and I converted it to <code>Swift3</code>) to get the same <code>UIImage</code> but grayscaled:</p>
  
  <pre><code>public func getGrayScale() -&gt; UIImage
  {
      let imgRect = CGRect(x: 0, y: 0, width: width, height: height)
  
      let colorSpace = CGColorSpaceCreateDeviceGray()
  
      let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue).rawValue)
      context?.draw(self.cgImage!, in: imgRect)
  
      let imageRef = context!.makeImage()
      let newImg = UIImage(cgImage: imageRef!)
  
      return newImg
  }
  </code></pre>
  
  <p>I can see the gray image but its quality is pretty bad... The only thing I can see that's related to the quality is <code>bitsPerComponent: 8</code> in the context contructor. However looking at Apple's doc, here is what I get:</p>
  
  <p><a href="https://i.stack.imgur.com/ESI7m.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ESI7m.png" alt="enter image description here"></a></p>
  
  <p>It shows that iOS only supports 8bpc... Thus why can't I improve the quality ?</p>