xcode 从 RGB 数据创建图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7235991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:50:09  来源:igfitidea点击:

Create image from RGB data?

iphonexcodeimagergb

提问by John P

I'm having real trouble with this. I have some raw rgb data, values from 0 to 255, and want to display it as an image on the iphone but can't find out how to do it. Can anyone help? I think i might need to use CGImageCreate but just don't get it. Tried looking at the class reference and am feeling quite stuck.

我真的遇到了这个问题。我有一些原始 rgb 数据,值从 0 到 255,想在 iphone 上将其显示为图像,但不知道如何操作。任何人都可以帮忙吗?我想我可能需要使用 CGImageCreate 但就是不明白。尝试查看课程参考资料,但感觉很卡。

All I want is a 10x10 greyscale image generated from some calculations and if there is an easy way to create a png or something that would be great.

我想要的只是通过一些计算生成的 10x10 灰度图像,如果有一种简单的方法来创建 png 或其他东西会很棒。

回答by justin

a terribly primitive example, similar to Mats' suggestion, but this version uses an external pixel buffer (pixelData):

一个非常原始的例子,类似于 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! :)

回答by Mats

Use CGBitmapContextCreate()to create a memory based bitmap for yourself. Then call CGBitmapContextGetData()to get a pointer for your drawing code. Then CGBitmapContextCreateImage()to create a CGImageRef.

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

I hope this is sufficient to get you started.

我希望这足以让你开始。

回答by w-m

On Mac OS you could do it with an NSBitmapImageRep. For iOS it seems to be a bit more complicated. I found this blog post though:

在 Mac OS 上,您可以使用 NSBitmapImageRep 来完成。对于iOS,它似乎有点复杂。不过我发现了这篇博文:

http://paulsolt.com/2010/09/ios-converting-uiimage-to-rgba8-bitmaps-and-back/

http://paulsolt.com/2010/09/ios-converting-uiimage-to-rgba8-bitmaps-and-back/