Loading a CGImage or NSImage from a file using a standard image format (jpeg, gif, png et.) is all very simple.
However, I now need to create a CGImage from an array in bytes in memory generated using libfreetype. Its really easy to create OpenGL textures from an array of formatted bytes, and I can see how to create a CGBitmapContext to write to. But I can’t seem to find an easy way to create a CGImage from a raw pixel array.
You can create a
CGDataProvider, and let CG request the necessary data from your provider, instead of writing to an image buffer.Here’s a very simple example that generates a black
CGImageof size 64×64.and getBytes is defined like this:
of course, you will want to implement the other callbacks (
skipForward,rewind,releaseInfo), and use a proper structure or object forinfo.For more information, check out the CGImage and CGDataProvider references.