Need example of how to create/manipulate image pixel data with iPhone SDK
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 开发者_Go百科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.
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);
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.
The books I referenced in this SO answer both contain sample code and demonstrations of image manipulations and updates via user interaction.
精彩评论