从RGB数据创建图像?
我遇到了麻烦。我有一些原始的RGB数据,从0到255的值,并希望在iPhone上显示它的图像,但无法找到如何做到这一点。谁能帮忙?我想我可能需要使用CGImageCreate,但只是不明白。试着看着课程参考,感觉很困难。从RGB数据创建图像?
我想要的只是从某些计算生成的10x10灰度图像,以及是否有简单的方法来创建png或其他很棒的东西。
在Mac OS上,您可以使用NSBitmapImageRep来完成。对于iOS来说,它似乎有点复杂。我发现这个博客帖子虽然:
http://paulsolt.com/2010/09/ios-converting-uiimage-to-rgba8-bitmaps-and-back/
感谢您的快速回复!我会研究它。 –
使用CGBitmapContextCreate()
创建一个基于内存的位图自己。然后致电CGBitmapContextGetData()
获取您的绘图代码的指针。然后CGBitmapContextCreateImage()
创建一个CGImageRef
。
我希望这足以让你开始。
好吧,在这里:我使用Xcode编程iOS和objective-c/coco。我陷入了困境,因为我从来没有使用任何石英或cgimage的东西,每个人都只是说使用CGBitmapContextCreate等,但我真的需要一个简单的例子。 –
另外,感谢您的链接,但我只是无法得到它的工作,并开始讨论它将如何帮助我。我尝试打印出的数据是为unsigned char创建的,然后用它来使uiimage期待它成为rgb数据,但我无法正确显示它。这是使用示例项目。当我把它放入我自己的时候,它没有缝合来正确加载我的图像,只是拒绝NSLog。 –
我发现另一个页面看起来不错,但我仍然没有收到它。我对此很新。 http://stackoverflow.com/questions/1579631/converting-rgb-data-into-a-bitmap-in-objective-c-cocoa –
一个非常原始的例子,类似垫的建议,但这个版本使用外部像素缓冲区(pixelData
):
const size_t Width = 10;
const size_t Height = 10;
const size_t Area = Width * Height;
const size_t ComponentsPerPixel = 4; // rgba
uint8_t pixelData[Area * ComponentsPerPixel];
// fill the pixels with a lovely opaque blue gradient:
for (size_t i=0; i < Area; ++i) {
const size_t offset = i * ComponentsPerPixel;
pixelData[offset] = i;
pixelData[offset+1] = i;
pixelData[offset+2] = i + i; // enhance blue
pixelData[offset+3] = UINT8_MAX; // opaque
}
// create the bitmap context:
const size_t BitsPerComponent = 8;
const size_t BytesPerRow=((BitsPerComponent * Width)/8) * ComponentsPerPixel;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef gtx = CGBitmapContextCreate(&pixelData[0], Width, Height, BitsPerComponent, BytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
// create the image:
CGImageRef toCGImage = CGBitmapContextCreateImage(gtx);
UIImage * uiimage = [[UIImage alloc] initWithCGImage:toCGImage];
NSData * png = UIImagePNGRepresentation(uiimage);
// remember to cleanup your resources! :)
看起来像我一直在寻找的东西。唯一的问题是我现在得到马赫O的错误:( –
我没有问题,我发布的程序。可能是一个新的问题? – justin
哦,我解决了它,谢谢!我不小心将CoreGraphics框架复制到我的项目文件夹。 ! –
你可以添加一些关于你的平台和哪种编程语言的细节吗?你提到CGImageCreate,你可以包含一个url吗? – eon
你怎么样给你一些细节,你卡在哪里?对于没有看到其他问题的人:http://stackoverflow.com/questions/7221604/create-image-from-nsarray-data/7221751#7221751 –