在Objective-C ++ Cocoa中将RGB数据转换成位图

我有一个RGB无符号字符的缓冲区,我想转换成一个位图文件,有没有人知道如何?

我的RGB浮动是以下格式

(0,0)],G [(0,0)],B [(0,0)],R [(0,1)],G [(0,1)],B [ 1)],R [(0,2)],G [(0,2)],B [(0,2)] …..

每个数据单元的值范围从0到255.任何人有任何想法,我怎么能做这个转换?

您可以使用CGBitmapContextCreate从原始数据创build位图上下文。 然后你可以从位图上下文创build一个CGImageRef并保存。 不幸的是CGBitmapContextCreate对数据的格式有点挑剔。 它不支持24位RGB数据。 开始时的循环将rgb数据调整为rgba,最后的值为零。 您必须包含并链接到ApplicationServices框架。

char* rgba = (char*)malloc(width*height*4); for(int i=0; i < width*height; ++i) { rgba[4*i] = myBuffer[3*i]; rgba[4*i+1] = myBuffer[3*i+1]; rgba[4*i+2] = myBuffer[3*i+2]; rgba[4*i+3] = 0; } CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef bitmapContext = CGBitmapContextCreate( rgba, width, height, 8, // bitsPerComponent 4*width, // bytesPerRow colorSpace, kCGImageAlphaNoneSkipLast); CFRelease(colorSpace); CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext); CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR("image.png"), kCFURLPOSIXPathStyle, false); CFStringRef type = kUTTypePNG; // or kUTTypeBMP if you like CGImageDestinationRef dest = CGImageDestinationCreateWithURL(url, type, 1, 0); CGImageDestinationAddImage(dest, cgImage, 0); CFRelease(cgImage); CFRelease(bitmapContext); CGImageDestinationFinalize(dest); free(rgba); 

借用nschmidt的代码来生成一个熟悉的,如果有人红眼的图像:

 int width = 11; int height = 8; Byte r[8][11]={ {000,000,255,000,000,000,000,000,255,000,000}, {000,000,000,255,000,000,000,255,000,000,000}, {000,000,255,255,255,255,255,255,255,000,000}, {000,255,255,255,255,255,255,255,255,255,000}, {255,255,255,255,255,255,255,255,255,255,255}, {255,000,255,255,255,255,255,255,255,000,255}, {255,000,255,000,000,000,000,000,255,000,255}, {000,000,000,255,255,000,255,255,000,000,000}}; Byte g[8][11]={ {000,000,255,000,000,000,000,000,255,000,000}, {000,000,000,255,000,000,000,255,000,000,000}, {000,000,255,255,255,255,255,255,255,000,000}, {000,255,255,000,255,255,255,000,255,255,000}, {255,255,255,255,255,255,255,255,255,255,255}, {255,000,255,255,255,255,255,255,255,000,255}, {255,000,255,000,000,000,000,000,255,000,255}, {000,000,000,255,255,000,255,255,000,000,000}}; Byte b[8][11]={ {000,000,255,000,000,000,000,000,255,000,000}, {000,000,000,255,000,000,000,255,000,000,000}, {000,000,255,255,255,255,255,255,255,000,000}, {000,255,255,000,255,255,255,000,255,255,000}, {255,255,255,255,255,255,255,255,255,255,255}, {255,000,255,255,255,255,255,255,255,000,255}, {255,000,255,000,000,000,000,000,255,000,255}, {000,000,000,255,255,000,255,255,000,000,000}}; char* rgba = (char*)malloc(width*height*4); int offset=0; for(int i=0; i < height; ++i) { for (int j=0; j < width; j++) { rgba[4*offset] = r[i][j]; rgba[4*offset+1] = g[i][j]; rgba[4*offset+2] = b[i][j]; rgba[4*offset+3] = 0; offset ++; } } CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef bitmapContext = CGBitmapContextCreate( rgba, width, height, 8, // bitsPerComponent 4*width, // bytesPerRow colorSpace, kCGImageAlphaNoneSkipLast); CFRelease(colorSpace); CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext); free(rgba); UIImage *newUIImage = [UIImage imageWithCGImage:cgImage]; UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 11,8)]; [iv setImage:newUIImage]; 

然后,添加子视图addSubview:iv将图像显示在您的视图中,当然,请执行必要的[releases]来保持一个干净的房子。