获取UIImage的像素颜色
如何获取 UIImage 中特定像素的 RGB 值?
How can I get the RGB value of a particular pixel in a UIImage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何获取 UIImage 中特定像素的 RGB 值?
How can I get the RGB value of a particular pixel in a UIImage?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
尝试这个非常简单的代码:
我曾经在迷宫游戏中检测墙壁(我需要的唯一信息是 Alpha 通道,但我包含了为您获取其他颜色的代码):
Try this very simple code:
I used to detect a wall in my maze game (the only info that I need is the alpha channel, but I included the code to get the other colors for you):
OnTouch
OnTouch
一些基于 Minas 答案的 Swift 代码。最初我有一些代码来计算像素步幅,但我已经更新了答案以使用 Desmond 的答案 中的 ComponentLayout 。我还将扩展名移至 CGImage。
Swift 5:
我试图重构它以使用范围,但这似乎不起作用,
Some Swift code based on Minas' answer. Originally I had some code to figure out the pixel stride, but I've updated the answer to use the ComponentLayout from Desmond's answer. I've also moved the extension to CGImage.
Swift 5:
I was trying to refactor it to use ranges, but this doesn't seem to work,
Swift 5 版本
此处给出的答案要么过时,要么不正确,因为它们没有考虑以下因素:
image 返回的点大小不同.size.width
/image.size.height
。UIView.drawHierarchy(in:afterScreenUpdates:)
方法可以生成BGRA图像。下面的代码是提供通用的 Swift 5 解决方案来获得对于所有此类特殊情况,像素的
UIColor
。该代码针对可用性和清晰度进行了优化,而不是针对性能进行了优化。Swift 5 version
The answers given here are either outdated or incorrect because they don't take into account the following:
image.size.width
/image.size.height
.UIView.drawHierarchy(in:afterScreenUpdates:)
method can produce BGRA images.CGImage
, the size of a pixel row in bytes can be greater than the mere multiplication of the pixel width by 4.The code below is to provide a universal Swift 5 solution to get the
UIColor
of a pixel for all such special cases. The code is optimized for usability and clarity, not for performance.您无法直接访问原始数据,但通过获取该图像的 CGImage 可以访问它。这是另一个问题的链接,可以回答您的问题以及您可能有的有关详细图像处理的其他问题:CGImage
You can't access the raw data directly, but by getting the CGImage of this image you can access it. here is a link to another question that answers your question and others you might have regarding detailed image manipulation : CGImage
这是一种在 UI 图像中获取像素颜色的通用方法,基于 Minas Petterson 的答案:
请注意,X 和 Y 可以交换;此函数直接访问底层位图,并且不考虑可能属于 UIImage 一部分的旋转。
Here's a generic method for getting pixel color in a UI image, building on Minas Petterson's answer:
Note that X and Y may be swapped; this function accesses the underlying bitmap directly and doesn't consider rotations that may be part of the UIImage.
米纳斯答案的 Swift 版本
Swift version of Minas answer
首先创建并附加点击手势识别器允许用户交互:
现在实现
-tapGesture:
这将适用于具有透明背景的 UILabel,如果这不是您想要的,您可以比较 alpha、红色、绿色,蓝色与
self.label.backgroundColor
...First of all create and attach tap gesture recognizer allow allow user interactions:
Now implement
-tapGesture:
This will works on UILabel with transparent background, if this is not what you want you can compare alpha, red, green, blue with
self.label.backgroundColor
...将这 2 个扩展方法添加到 Desmond 解决方案中:
Add those 2 extension methods to Desmond solution: