0

I need to convert my bitmap from the normal camera format of kCVPixelFormatType_32BGRA to the kCVPixelFormatType_24RGB format so it can be consumed by a 3rd party library.

How can this be done?

My c# code looks like this in an effort of doing it directly with the byte data:

byte[] sourceBytes = UIImageTransformations.BytesFromImage(sourceImage);
// final source is to be RGB
byte[] finalBytes = new byte[(int)(sourceBytes.Length * .75)];
int length = sourceBytes.Length;
int finalByte = 0;
for (int i = 0; i < length; i += 4) 
{
    byte blue = sourceBytes[i];
    byte green = sourceBytes[i + 1];
    byte red = sourceBytes[i + 2];

    finalBytes[finalByte] = red;
    finalBytes[finalByte + 1] = green;
    finalBytes[finalByte + 2] = blue;
    finalByte += 3;
}

UIImage finalImage = UIImageTransformations.ImageFromBytes(finalBytes);

However I'm finding that my sourceBytes length is not always divisible by 4 which doesn't make any sense to me.

Aaron Bratcher
  • 6,051
  • 2
  • 39
  • 70
  • This might help: http://stackoverflow.com/questions/13201084/how-to-convert-a-kcvpixelformattype-420ypcbcr8biplanarfullrange-buffer-to-uiimag – matt Sep 30 '15 at 18:23
  • @matt Thanks for the suggestion. Unfortunately I can already go from my camera format to a UIImage. I need to change the format itself. – Aaron Bratcher Sep 30 '15 at 18:35
  • 1
    I'm not sure what that means. A UIImage has a fixed format. You can make a bitmap context as a way of handing the data over to some other library, and that is what the example shows you how to do. – matt Sep 30 '15 at 18:43

0 Answers0