从RGB数据创建图像? [英] Create image from RGB data?

查看:108
本文介绍了从RGB数据创建图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

一个非常原始的例子,类似于Mats的建议,但这个版本使用外部像素buffer( pixelData ):

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! :)

这篇关于从RGB数据创建图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆