Converting RGB data into a bitmap in Objective-C++ Cocoa
You can use CGBitmapContextCreate to make a bitmap context from your raw data. Then you can create a CGImageRef from the bitmap context and save it. Unfortunately CGBitmapContextCreate is a little picky about the format of the data. It does not support 24-bit RGB data. The loop at the beginning swizzles the rgb data to rgba with an alpha value of zero at the end. You have to include and link with ApplicationServices framework.
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 likeCGImageDestinationRef dest = CGImageDestinationCreateWithURL(url, type, 1, 0);CGImageDestinationAddImage(dest, cgImage, 0);CFRelease(cgImage);CFRelease(bitmapContext);CGImageDestinationFinalize(dest);free(rgba);
Borrowing from nschmidt's code to produce a familiar, if someone red-eyed image:
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];
Then, addSubview:iv
to get the image into your view and, of course, do the obligatory [releases]
to keep a clean house.