来自字节数组的 CGImage

编程入门 行业动态 更新时间:2024-10-27 11:24:03
本文介绍了来自字节数组的 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),并使用适当的结构或对象信息.

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

发布评论

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

>www.elefans.com

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