0

I have a float array "MyColors[100][4]" (4 because of rgb & a for each of MyColors). I want to turn it into a bitmap to make a UIImage out of it. I've experimented with some code but really don't know what I'm doing. How can I make my 100x4 array into something a bitmap can use?

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
Andrew Savage
  • 103
  • 1
  • 7

1 Answers1

0

You could use CGImageCreate function. That creates a CGImageRef, which then can be used to create a UIImage:

UIImage *uiImage = [[UIImage alloc] initWithCGImage:cgImage];

CGImageCreate "provider" parameter (of type CGDataProviderRef) is a thing that provides the image data, so first you need to create that from your array. One example of using CGDataProviderCreateWithData is here.

It expects colors in a certain specified format. The supported formats are described here "Color Spaces and Bitmap Layout". It seems that you can use floats if you premultiply colors with alpha, and use the kCGBitmapFloatComponents flag, but I would rather simplify it and just use integers instead for color components.

battlmonstr
  • 5,841
  • 1
  • 23
  • 33
  • thanks that was very helpful, I was able to get the process started and can create an image, but it comes out completely wrong but with hints of the colors its supposed to have. MyColor array is made of Ints between 0-255 – Andrew Savage Mar 12 '18 at 18:02
  • Great. I guess you should choose one of the formats they support, and convert your data to what they expect. For example kCGImageAlphaLast is the one where you have each color as an 4 bytes int, where each byte is a color component (0-255). – battlmonstr Mar 13 '18 at 19:05