How do I convert a hex array into a UIImage?

前端 未结 2 493
醉梦人生
醉梦人生 2021-01-16 15:52

There are several unanswered questions related to printing images dynamically with P25mi, none with accepted answers. A couple links below.

How to convert image to

2条回答
  •  迷失自我
    2021-01-16 16:24

    Its easiest to start with a black and white image (I used .png) 4 bytes are allocated to each pixel, the data of which you can get by using CgContextRef and CgContextDrawImage (I used the CGBitmapContextCreate method to populate an array of pixel data as well as create a black and white image from the edited data array. I averaged the rgb values for each pixel and then changed all pixel values to black or white (alpha == 255) depending on the average of rgb's.

    Then you need to package every 8 pixels into 1 print byte (thus for a pixel of 4 bytes, look at a set of 32 bytes in the data array and set the binary values of the print byte on or off depending on whether a pixel is black (add pow(2, 7 - (position of pixel in 8 pixel sequence) ) or white (do nothing).

    I converted a .png image to black and white .png and then to binary and printed the values on my screen. Try to keep the width of your .png in multiples of 8 or you'll have trouble (since you need to convert 8 bytes into 1 byte.

提交回复
热议问题