Need an example of how to create / manipulate image pixel data with iPhone SDK

Look for a simple example or link to a tutorial.

Let's 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. Suppose the array values ​​are intensity data and will update the image in grayscale. Suppose the array values ​​are in the range 0 to 255, or that I will convert it to this range.

This is not for animation. Rather, the image will be updated based on user interaction. This is what I know how to succeed in Java, but I am very new to programming on the iPhone. I was looking for some information about CGImage and UIImage, but I do not understand where to start.

Any help would be appreciated.

+4
source share
3 answers

I have an example code from one of my applications that takes data stored as an unsigned char array 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); 
+4
source

If your color space is RGB and you need to pass the alpha value of kCGImageAlphaPremultipliedLast as the last parameter to the CGBitmapContextCreate function.

Do not use kCGImageAlphaLast , this will not work because raster contexts do not support alpha, which is not multiplied.

+1
source

The books I referenced in this SO answer contain sample code and demonstrations of image manipulation and updates through user interaction.

0
source

Source: https://habr.com/ru/post/1308718/


All Articles