本文介绍了从字节数组的CGImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用标准图片格式(jpeg,gif,png等)从文件加载CGImage或NSImage非常简单。

Loading a CGImage or NSImage from a file using a standard image format (jpeg, gif, png et.) is all very simple.

但是,现在我需要从使用libfreetype生成的内存中的字节数组中创建一个CGImage。它真的很容易从格式化的字节数组创建OpenGL纹理,我可以看到如何创建一个CGBitmapContext来。但我似乎找不到一个简单的方法从原始像素数组创建一个CGImage。

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.

推荐答案

CGDataProvider ,让CG从您的提供者请求必要的数据,而不是写入图像缓冲区。

You can create a CGDataProvider, and let CG request the necessary data from your provider, instead of writing to an image buffer.

一个非常简单的例子,生成大小为64x64的黑色 CGImage

Here's a very simple example that generates a black CGImage of size 64x64.

CGDataProviderSequentialCallbacks callbacks;
callbacks.getBytes = getBytes;

CGDataProviderRef provider = CGDataProviderCreateSequential(NULL, &callbacks);
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGImageRef img = CGImageCreate(64,                         // width
                               64,                         // height
                               8,                          // bitsPerComponent
                               24,                         // bitsPerPixel
                               64*3,                       // bytesPerRow
                               space,                      // colorspace
                               kCGBitmapByteOrderDefault,  // bitmapInfo
                               provider,                   // CGDataProvider
                               NULL,                       // decode array
                               NO,                         // shouldInterpolate
                               kCGRenderingIntentDefault); // intent

CGColorSpaceRelease(space);
CGDataProviderRelease(provider);

// use the created CGImage

CGImageRelease(img);

,getBytes的定义如下:

and getBytes is defined like this:

size_t getBytes(void *info, void *buffer, size_t count) {
    memset(buffer, 0x00, count);
    return count;
}

当然,您将需要实现其他回调( skipForward rewind releaseInfo ),并使用适当的结构或对象 info

of course, you will want to implement the other callbacks (skipForward, rewind, releaseInfo), and use a proper structure or object for info.

有关详细信息,请查看和参考。

For more information, check out the CGImage and CGDataProvider references.

这篇关于从字节数组的CGImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 03:50