Convertendo dados RGB em um bitmap em cocoa Objective-C ++

Eu tenho um buffer de char unsigned RGB que gostaria de converter em um arquivo de bitmap, alguém sabe como?

Meu float RGB é do seguinte formato

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

Os valores para cada unidade de dados variam de 0 a 255. Alguém tem alguma idéia de como eu posso fazer essa conversão?

Você pode usar CGBitmapContextCreate para criar um contexto de bitmap a partir de seus dados brutos. Em seguida, você pode criar um CGImageRef a partir do contexto de bitmap e salvá-lo. Infelizmente CGBitmapContextCreate é um pouco exigente sobre o formato dos dados. Não suporta dados RGB de 24 bits. O loop no início migra os dados rgb para rgba com um valor alfa de zero no final. Você precisa include e vincular a estrutura do 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); 

Emprestando do código de nschmidt para produzir uma imagem familiar, se alguém de olhos vermelhos:

 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]; 

Então, addSubview:iv para colocar a imagem em sua visão e, claro, fazer as [releases] obrigatórias para manter uma casa limpa.