tags:

views:

298

answers:

3

Looking for a simple example or link to a tutorial.

Say I have a bunch of values stored in an array. I would like to create an image and update the image data from my array. Assume the array values are intensity data and will be updating a grayscale image. Assume the array values are between 0 and 255 -- or that I will convert it to that range.

This is not for purposes of animation. Rather the image would be updated based on user interaction. This is something I know how to do well in Java, but am very new to iPhone programming. I've googled some information about CGImage and UIImage -- but am confused as to where to start.

Any help would be appreciated.

A: 

The books I referenced in this SO answer both contain sample code and demonstrations of image manipulations and updates via user interaction.

mmr
+1  A: 

I have sample code from one of my apps that takes data stored as an array of unsigned char and turns it into a UIImage:

// unsigned char *bitmap; // This is the bitmap data you already have.
// int width, height; // bitmap length should equal width * height

// Create a bitmap context with the image data
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(bitmap, width, height, 8, width, colorspace, kCGImageAlphaNone);
CGImageRef cgImage = nil;
if (context != nil) {
    cgImage = CGBitmapContextCreateImage (context);
    CGContextRelease(context);
}
CGColorSpaceRelease(colorspace);

// Release the cgImage when done
CGImageRelease(cgImage);
lucius
A: 

If your colorspace is RGB and you need to account alpha value pass kCGImageAlphaPremultipliedLast as last parameter to CGBitmapContextCreate function.

Don't use kCGImageAlphaLast, this will not work because bitmap contexts do not support alpha that isn't premultiplied.

plug-in