title: how to create grayscale image from nsimage in swift?
body: <p>I created two applications: one for mac and one for iPhone. iPhone sends the video frames it captured to mac using MultipeerConnectivity framework. I have managed to find code for converting an UIimage to grayscale using this code:</p>
  
  <pre><code>func convertToGrayScale(image: UIImage) -&gt; UIImage {
          let imageRect:CGRect = CGRectMake(0, 0, image.size.width, image.size.height)
          let colorSpace = CGColorSpaceCreateDeviceGray()
          let width = image.size.width
          let height = image.size.height
          let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.None.rawValue)
          let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo.rawValue)
          CGContextDrawImage(context, imageRect, image.CGImage)
          let imageRef = CGBitmapContextCreateImage(context)
          let newImage = UIImage(CGImage: imageRef!)
          return newImage
      }
  </code></pre>
  
  <p>In the code below, it sends the video frame to Mac:</p>
  
  <pre><code>func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
          let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
          CVPixelBufferLockBaseAddress(imageBuffer!, kCVPixelBufferLock_ReadOnly)
          let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer!)
          let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
          let width = CVPixelBufferGetWidth(imageBuffer!)
          let height = CVPixelBufferGetHeight(imageBuffer!)
          CVPixelBufferUnlockBaseAddress(imageBuffer!, 0)
          let colorSpace = CGColorSpaceCreateDeviceRGB()
          let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
          let context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, bitmapInfo.rawValue)
          let quarzImage = CGBitmapContextCreateImage(context)
          let image = UIImage(CGImage: quarzImage!)
          let grayImage = convertToGrayScale(image)
          let data: NSData = UIImagePNGRepresentation(grayImage)!
          delegate?.recievedOutput(data)
      }
  </code></pre>
  
  <p>The delegate method is just sending the data using session.sendData()</p>
  
  <p>So, here comes to the Mac side. When mac received NSData, I created an NSImage from the data and created a .png image file using this code:</p>
  
  <pre><code>func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID) {
          let image: NSImage = NSImage(data: data)!.imageRotatedByDegreess(270)
          let cgRef = image.CGImageForProposedRect(nil, context: nil, hints: nil)
          let representation = NSBitmapImageRep(CGImage: cgRef!)
          let pngData = representation.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [NSImageCompressionFactor: 1.0])
          pngData?.writeToFile("/Users/JunhongXu/Desktop/image/\(result.description).png", atomically: true)
          result[4]++
          self.delegate?.presentRecievedImage(image)
      }
  </code></pre>
  
  <p>Although the image is like the picture below, when I checked my image file property, it is in RGB format. How can I change the ColorSpace of my NSImage to grayscale instead of RGB?</p>
  
  <p><a href="http://i.stack.imgur.com/Y5l7B.png" rel="noreferrer">enter image description here</a></p>