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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With