Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS bitmap image creation -- simple example

Say I want to create an image 100 pixels wide and 200 pixels high, with a 100x100 red square at the top and a 100x100 brown square [RGB components 0.6, 0.4, 0.2] on the bottom. And say I want to do it as a bitmap, with a straight C function spitting out the components for each pixel, one pixel at a time. [There are easier ways to create that image, but the point here is to understand iOS bitmaps -- next time, I might have arbitrary values for each pixel.] How would I do that?

like image 755
William Jockusch Avatar asked Apr 19 '11 02:04

William Jockusch


1 Answers

The APIs you want to use are documented in Apple's CGBitmapContext Reference.

Use CGBitmapContextCreate to create a bitmap context from a block of memory of size

height * width * pixelSize

(pixelSize is usually 4 bytes for ARGB.) Then, after playing with the pixels, use CGBitmapContextCreateImage to create an imageRef from the bitmap context, and you have your image. You can assign this image to the contents of a CALayer for viewing, or draw the image inside a drawRect.

You can use C array syntax to access pixels:

myPixelPtr = &bitmap[pixelSize * (x + y * bitmapWidth)];

Your pixel type can a C struct of 4 unsigned bytes, one each for ARGB, or just raw bytes.

like image 160
hotpaw2 Avatar answered Sep 28 '22 16:09

hotpaw2