从 RGB 数据创建图像?

发布于 2024-12-01 22:46:34 字数 191 浏览 1 评论 0原文

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悍妇囚夫 2024-12-08 22:46:34

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

a terribly primitive example, similar to Mats' suggestion, but this version uses an external pixel buffer (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! :)
不顾 2024-12-08 22:46:34

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

我希望这足以让您开始。

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.

I hope this is sufficient to get you started.

遮云壑 2024-12-08 22:46:34

在 Mac OS 上,您可以使用 NSBitmapImageRep 来完成此操作。对于iOS来说,似乎有点复杂。我找到了这篇博文:

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

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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文