0

I am working with an API which gives me an image as the example below. I am unsure how I can convert this array of values into an image. I have not seen this before.

Does anyone know how I can make the following array of values into a UIImage?

[255,216,255,224,0,16,74,70,73,70,0,1,1,1,0,96,...]

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
simonbs
  • 7,932
  • 13
  • 69
  • 115
  • if it`s public API then mention that API name so someone gives you proper direction. or mention which encoding method use when API send image. – AJPatel Apr 16 '12 at 05:52
  • I am sorry that I cannot give any more information but the API is not public. My hope is that somebody have seen an image represented in this way before. – simonbs Apr 16 '12 at 05:56
  • it seems like you are getting NSData in the response, have you tried- [UIImage imageWithData:imageData]; – rishi Apr 16 '12 at 06:02
  • i think if it is NSData, the return value will be in bytes, right? – Charan Apr 16 '12 at 06:05
  • I don't think it's NSData. When doing `[UIImage imageWithData:imageData]` the application will crash with an `NSInvalidArgumentException`. The response (and the array in the question) is really just an array of integers which I need to interpret. – simonbs Apr 16 '12 at 06:21
  • it seems that it is a array of bytes... what type of encoding server is using? – Inder Kumar Rathore Apr 16 '12 at 06:54
  • The API returns an XML document which contains a JSON string (not ideal, I know). This JSON string contains the array of integers in the question. The XML document contains the following line, so it seems the encoding is UTF-8, right? `` – simonbs Apr 16 '12 at 07:20

1 Answers1

1

That looks like straight pixel buffer data, so you will have to guess the format (probably RBGA?) and the image dimensions (hopefully it's fixed/guessable/included in the format). If you guess correctly you should be able to create a CIImage with CGDataProviderCreateWithData;

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);
//a reasonable guess
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;

CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, kCGBitmapByteOrderDefault, provider, NULL, NO, kCGRenderingIntentDefault);
UIImage *newUIImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef); 
CGDataProviderRelease(provider);
voromax
  • 3,369
  • 2
  • 30
  • 53
Steven Veltema
  • 2,140
  • 15
  • 18
  • I think that you are right. I'm trying to create a method in which I pass the `NSArray` and a size for the image. Then the method returns the `UIImage`. I'm not sure what to do with the `NSArray`. Can you please tell me how to use it with the code you posted? – simonbs Apr 16 '12 at 10:46
  • Here is a pretty complete example of the above with buffer creation included: http://stackoverflow.com/questions/1579631/converting-rgb-data-into-a-bitmap-in-objective-c-cocoa – Steven Veltema Apr 17 '12 at 02:21