简体   繁体   中英

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 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.

我在这个SO答案中引用的书籍都包含示例代码以及通过用户交互进行图像处理和更新的演示。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM