从 CGImage 获取 RGB 像素数据
我正在尝试从 CGImage 访问像素数据。我希望能够以整数形式访问 RGB 值。我想我已经用这段代码完成了:
UIImage* theImage = [UIImage imageNamed:@"rgb.png"];
CGImageRef cgImageRef = CGImageRetain(theImage.CGImage);
CFDataRef* imageData = CGDataProviderCopyData(CGImageGetDataProvider(cgImageRef));
NSLog(@"the data = %@", imageData);
然后记录:
data = <010002fe fffdff02 0200fe04 0003fc>
该图像是一个 5x1 png,按顺序包含黑色、白色、红色、绿色和蓝色像素。
我真的不明白我在这里看什么。我怎样才能获得 RGB 值数组或类似的值,以便我可以使用它们。
谢谢, 富有的
I am trying to access pixel data from a CGImage. I want to be able to access the RGB values as integers. I think I am nearly there with this code:
UIImage* theImage = [UIImage imageNamed:@"rgb.png"];
CGImageRef cgImageRef = CGImageRetain(theImage.CGImage);
CFDataRef* imageData = CGDataProviderCopyData(CGImageGetDataProvider(cgImageRef));
NSLog(@"the data = %@", imageData);
This then logs:
the data = <010002fe fffdff02 0200fe04 0003fc>
The image is a 5x1 png containing a black, white, red, green and blue pixel in that order.
I don't really understand what I am looking at here. How can I get an array of RGB values, or something similar so I can work with them.
Thanks,
Rich
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是一些示例代码,它将打印一堆有关图像的相关信息,以及图像像素数据的转储。它适用于具有 Alpha 通道的图像,以及不具有 Alpha 通道的图像。该代码甚至可以处理不在 RGB 颜色空间中的图像,尽管我怀疑您是否可能在 iOS 上获得这些图像。
将其复制粘贴到您的项目中,并尝试针对一些图像文件运行它,然后也许您可以根据您的需要调整它
以下是我尝试过的两个图像的一些示例输出。它们都是 5x3 RGB。 “a.png”图像具有 Alpha 通道,而“b.rgb”则没有。
Here's some sample code that will print a bunch of relevant info about an image, as well as a dump of the image's pixel data. It will work on images with alpha channels, as well as images without. The code will even work on images that are not in the RGB color space, though I doubt you'd be likely to get any of those on iOS.
Copy-paste it into your project and try to run it against a few of your image files, and then perhaps you can adapt it to your needs
Here's some sample output on two images I tried. They're both 5x3 rgb. The "a.png" image has an alpha channel, while the "b.rgb" does not.