title: how to create a bitmap image from the byte array in swift
body: <p>I have a byte array which comes from a finger print sensor device. I wanted to create a bitmap out of it. I have tried few examples but all I am getting is a nil UIImage.</p>
  
  <p>If there are any steps to do that, pls tell me.</p>
  
  <p>Thanks.</p>
  
  <p>This is what my func does:</p>
  
  <pre><code>func didFingerGrabDataReceived(data: [UInt8]) {
      if data[0] == 0 {
          let width1 = Int16(data[0]) &lt;&lt; 8
          let finalWidth = Int(Int16(data[1]) | width1)
  
          let height1 = Int16(data[2]) &lt;&lt; 8
          let finalHeight = Int(Int16(data[3]) | height1)
  
          var finalData:[UInt8] = [UInt8]()
  
  // i dont want the first 8 bytes, so am removing it
  
          for i in 8 ..&lt; data.count {
              finalData.append(data[i])
          }
  
       dispatch_async(dispatch_get_main_queue()) { () -&gt; Void in
  
          let msgData = NSMutableData(bytes: finalData, length: finalData.count)
  
          let ptr = UnsafeMutablePointer&lt;UInt8&gt;(msgData.mutableBytes)
  
  
          let colorSpace = CGColorSpaceCreateDeviceGray()
          if colorSpace == nil {
              self.showToast("color space is nil")
              return
          }
  
          let bitmapContext = CGBitmapContextCreate(ptr, finalWidth, finalHeight, 8, 4 * finalWidth, colorSpace,  CGImageAlphaInfo.Only.rawValue);
  
          if bitmapContext == nil {
              self.showToast("context is nil")
              return
          }
  
          let cgImage=CGBitmapContextCreateImage(bitmapContext);
          if cgImage == nil {
              self.showToast("image is nil")
              return
          }
  
          let newimage = UIImage(CGImage: cgImage!)
  
              self.imageViewFinger.image = newimage
          }
  }
  </code></pre>
  
  <p>I am getting a distorted image. someone please help</p>