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,...]

7
  • 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. Commented Apr 16, 2012 at 5: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. Commented Apr 16, 2012 at 5:56
  • it seems like you are getting NSData in the response, have you tried- [UIImage imageWithData:imageData]; Commented Apr 16, 2012 at 6:02
  • i think if it is NSData, the return value will be in bytes, right? Commented Apr 16, 2012 at 6: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. Commented Apr 16, 2012 at 6:21

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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?
Here is a pretty complete example of the above with buffer creation included: stackoverflow.com/questions/1579631/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.