5

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.

John P
  • 51
  • 1
  • 3
  • can you add some details about your platform and which programming language? you mention CGImageCreate, can you include a url? –  Aug 29 '11 at 21:09
  • 1
    How about you give some details where you're stuck? For anyone who didn't see the other question: http://stackoverflow.com/questions/7221604/create-image-from-nsarray-data/7221751#7221751 – w-m Aug 29 '11 at 21:13

3 Answers3

14

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! :)
justin
  • 104,054
  • 14
  • 179
  • 226
  • that looks like what i was looking for. Only problem is i now get mach-O errors :( – John P Aug 30 '11 at 08:49
  • i had no issues with the program i posted. a new question, perhaps? – justin Aug 30 '11 at 08:51
  • oh i solved it, thanks! I had accidentally copied the CoreGraphics framework to my project folder. oops! – John P Aug 30 '11 at 08:53
  • It's working, but would you mind briefly explaining how I change the pixelData to get what I want to display? If you don't want to thanks anyway for the code! – John P Aug 30 '11 at 08:57
  • `pixelData` is a contiguous rgba pixel component buffer - very common in C graphics. its memory layout (increasing monotonically) is rgbargbargbargbargba... you can see how i generated the blue gradient by setting the elements of `pixelData` in the sample. i'm not sure what your question is specifically... perhaps this will help(?): http://en.wikipedia.org/wiki/Pixels – justin Aug 30 '11 at 09:25
  • well i have all my rgb values as an array and would like to pass them to something like this to have them drawn. – John P Aug 30 '11 at 09:34
  • if that array is the 10x10 pixel value source, then you could pass that to `CGBitmapContextCreate` instead of `pixelData`. either that or copy the pixels from your buffer to `pixelData`. – justin Aug 30 '11 at 09:37
3

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.

Mats
  • 8,528
  • 1
  • 29
  • 35
  • Ok here goes: I'm programming for iOS using Xcode and objective-c / coco. I'm getting stuck because I've never used any of the quartz or cgimage stuff before and everyone just says oh use CGBitmapContextCreate etc but I really need an simple example. – John P Aug 29 '11 at 21:52
  • Also, thanks for the link but I just couldn't get it working and began ondering how it would help me. I tried printing out the data that was created for the unsigned char that was then used to make a uiimage expecting it to be rgb data but I couldn't get it to display properly. That was using the example project. When I put it into my own it didn't seam to load my image properly and just refused to NSLog anything. – John P Aug 29 '11 at 21:56
  • I found another page that looks good but I'm still not getting it. I'm quite new to this. http://stackoverflow.com/questions/1579631/converting-rgb-data-into-a-bitmap-in-objective-c-cocoa – John P Aug 29 '11 at 22:11
  • Also, I just tried getting data from a UIImage but it just logs "PNG". This is my code: UIImage *img = [UIImage imageNamed:@"img.png"]; NSData *dataObj = UIImagePNGRepresentation(img); NSString *aStr = [[NSString alloc] initWithData:dataObj encoding:NSASCIIStringEncoding]; NSLog(@"%@",aStr); – John P Aug 29 '11 at 22:51
  • PNGRepresentation is basically as a file format. That's not what you want. (Or at least, creating a PNG on disk is different from creating an image.) – David Dunham Aug 29 '11 at 23:23
1

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/

w-m
  • 10,772
  • 1
  • 42
  • 49