从字节数组的CGImage

编程入门 行业动态 更新时间:2024-10-27 13:31:01
本文介绍了从字节数组的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.

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

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

更多推荐

从字节数组的CGImage

本文发布于:2023-07-30 16:26:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1251038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字节   CGImage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!