从RGB数据创建图像?

Joh*_*n P 5 iphone rgb xcode image

我遇到了这个问题.我有一些原始的rgb数据,值从0到255,并希望将其显示为iphone上的图像,但无法找到如何操作.有人可以帮忙吗?我想我可能需要使用CGImageCreate,但只是不明白.试着看着班级参考,感觉很困难.

我想要的是从一些计算生成的10x10灰度图像,如果有一种简单的方法来创建一个png或者很棒的东西.

jus*_*tin 13

一个非常原始的例子,类似于Mats的建议,但这个版本使用外部像素缓冲区(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! :)
Run Code Online (Sandbox Code Playgroud)


Mat*_*ats 3

用于CGBitmapContextCreate()为自己创建基于内存的位图。然后调用CGBitmapContextGetData()以获取绘图代码的指针。然后CGBitmapContextCreateImage()创建一个CGImageRef.

我希望这足以让您开始。