我正在使用一个API,它给了我一个图像,如下例所示。我不确定如何将这些数值转换为图像。我以前没见过这个。来自阵列的UIImage
有谁知道我可以如何将下面的值数组放入UIImage中?
[255,216,255,224,0,16,74,70,73,70,0,1,1,1,0,96,...]
我正在使用一个API,它给了我一个图像,如下例所示。我不确定如何将这些数值转换为图像。我以前没见过这个。来自阵列的UIImage
有谁知道我可以如何将下面的值数组放入UIImage中?
[255,216,255,224,0,16,74,70,73,70,0,1,1,1,0,96,...]
这看起来像直像素缓冲区的数据,所以你将不得不猜测格式(可能是,RGBA?)和图像尺寸(希望它的固定/猜测的/纳入格式)。如果你猜对了,你应该可以用CGDataProviderCreateWithData创建一个CIImage;
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);
我认为你是对的。我试图创建一个方法,我通过'NSArray'和图像的大小。然后该方法返回'UIImage'。我不知道如何处理'NSArray'。你能告诉我如何将它与你发布的代码一起使用吗? – simonbs 2012-04-16 10:46:45
下面是一个上面的缓冲区创建包括一个非常完整的例子:http://stackoverflow.com/questions/1579631/converting-rgb-data-into-a-bitmap-in-objective-c-cocoa – 2012-04-17 02:21:50
如果是公共API,则提及该API名称,以便有人给你正确的方向。或者提及API发送图像时使用哪种编码方法。 – AJPatel 2012-04-16 05:52:51
很抱歉,我无法提供更多信息,但该API不公开。我的希望是有人看到过以这种方式表现出来的形象。 – simonbs 2012-04-16 05:56:04
它看起来像你在响应中获取NSData,你有没有尝试 - [UIImage imageWithData:imageData]; – rishi 2012-04-16 06:02:05