title: How to get image data for printing on thermal printer using ESC/POS?
body: <p>First I initialize printer(ESC @ ):</p>
  
  <pre><code>addBytesCommand(Data(bytes: [0x1B, 0x40]))
  </code></pre>
  
  <p>Then I set the line feed amount(ESC 3 n):</p>
  
  <pre><code>addBytesCommand(Data(bytes: [0x1B, 0x33, 24]))
  </code></pre>
  
  <p>Then I'm trying to use (ESC *) command to print image:</p>
  
  <pre><code>addBytesCommand(Data(bytes: [0x1B, 0x2A, 33]))
  
  let image = #imageLiteral(resourceName: "icon_circle_remove_filled")
  let imageWidth = image.size.width
  let lowByte = UInt8(Int(imageWidth) &amp; 0x00ff)
  let highByte = UInt8((Int(imageWidth) &amp; 0xff00) &gt;&gt; 8)
  
  addBytesCommand(Data(bytes: [lowByte, highByte]))
  </code></pre>
  
  <p>The logic for lowByte and highByte I used from this explanation:
  <a href="http://new-grumpy-mentat.blogspot.com/2014/06/java-escpos-image-printing.html" rel="nofollow noreferrer">ESC/POS Image Printing</a>.<br/>
  After that I'm trying to get image image data to send to printer using this extension for UIImage. </p>
  
  <pre><code>extension UIImage {
  func pixelData() -&gt; [UInt8]? {
      guard let cgImage = self.cgImage else { return nil }
  
      let size = self.size
      let bitsPerPixel = UInt8(cgImage.bitsPerPixel)
      let bitsPerComponent = cgImage.bitsPerComponent
      let bytesPerRow = cgImage.bytesPerRow
  
      let dataSize = bytesPerRow * cgImage.height
      var pixelData = [UInt8](repeating: 0, count: dataSize)
      let colorSpace = CGColorSpaceCreateDeviceGray()
      let context = CGContext(data: &amp;pixelData,
                              width: Int(size.width),
                              height: Int(size.height),
                              bitsPerComponent: bitsPerComponent,
                              bytesPerRow: bytesPerRow,
                              space: colorSpace,
                              bitmapInfo: CGImageAlphaInfo.alphaOnly.rawValue)
      context?.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
  
      return pixelData
  }
  </code></pre>
  
  <p>And that's where I have problems. I don't understand what <code>pixelData</code> contains exactly and how can I transform it to correct type of data? 
  What is the correct way to get bitmap information for printing?</p>