在 Objective-C 中将 RGB 数据转换为位图可可
我有一个 RGB 无符号字符缓冲区,我想将其转换为位图文件,有人知道如何吗?
我的 RGB 浮点数的格式如下
R [(0,0)], G[(0,0)], B[(0,0)],R [(0,1)], G[(0,1) )], B[(0,1)], R [(0,2)], G[(0,2)], B[(0,2)] .....
各数据单元的取值范围从 0 到 255。有人知道我如何进行这种转换吗?
I have a buffer of RGB unsigned char that I would like converted into a bitmap file, does anyone know how?
My RGB float is of the following format
R [(0,0)], G[(0,0)], B[(0,0)],R [(0,1)], G[(0,1)], B[(0,1)], R [(0,2)], G[(0,2)], B[(0,2)] .....
The values for each data unit ranges from 0 to 255. anyone has any ideas how I can go about making this conversion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 CGBitmapContextCreate 从原始数据创建位图上下文。然后您可以从位图上下文创建 CGImageRef 并保存它。不幸的是 CGBitmapContextCreate 对数据的格式有点挑剔。它不支持 24 位 RGB 数据。开始时的循环将 rgb 数据混合为 rgba,最后将 alpha 值设置为零。您必须包含并链接到 ApplicationServices 框架。
You can use CGBitmapContextCreate to make a bitmap context from your raw data. Then you can create a CGImageRef from the bitmap context and save it. Unfortunately CGBitmapContextCreate is a little picky about the format of the data. It does not support 24-bit RGB data. The loop at the beginning swizzles the rgb data to rgba with an alpha value of zero at the end. You have to include and link with ApplicationServices framework.
借用 nschmidt 的代码来生成一个熟悉的、如果有人红了眼睛的图像:
然后,
addSubview:iv
将图像放入您的视图中,当然,执行必要的[releases]< /code> 保持房子干净。
Borrowing from nschmidt's code to produce a familiar, if someone red-eyed image:
Then,
addSubview:iv
to get the image into your view and, of course, do the obligatory[releases]
to keep a clean house.