OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 UIImage 中获取指定颜色的位置? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:34
标题: ios - 如何在 UIImage 中获取指定颜色的位置?

我想获取 UIImage 中像素的位置,其颜色与指定颜色匹配。

例如,我想找到这个颜色的像素的位置(x,y):[UIColor colorWithRed:255 green:0 blue:0 alpha:1]

CGPoint 点 = getFirstPixelOfColor([UIColor colorWithRed:255 green:0 blue:0 alpha:1].CGColor);



Best Answer-推荐答案


添加 UIImage-Utils到你的项目。然后像这样获取 x,y 的颜色:

CGFloat imageWidth = image.size.width;
NSData *imageData = [UIImage bytesFromImage:image]
Byte *bytes = (Byte *)imageData.bytes;
CGFloat red    = bytes[redOffset   (x, y, imageWidth)] / 255.0f;
CGFloat green  = bytes[greenOffset (x, y, imageWidth)] / 255.0f;
CGFloat blue   = bytes[blueOffset  (x, y, imageWidth)] / 255.0f;
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];

存储与您要查找的颜色匹配的颜色的 x,y。对图像中的每个 x,y 重复此操作。

关于ios - 如何在 UIImage 中获取指定颜色的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18170398/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4