我有一个带有 CIColorPosterize CIFilter 的分色图像。该图像包含几种颜色。 在这一点上,我想从这个图像中提取一个带有 n 种颜色的 UIColor 数组。 有一个函数可以做这样的事情吗? 否则,哪种方法可能是实现这一点的有效方法?
谢谢各位专家!
最佳答案
你可以试试这个..
CGImageRef imageRef = [yourImage CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
for (int i = 0 ; i < count ; byteIndex += 4,++i)
{
CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
[result addObject:acolor];
}
free(rawData);
NSLog(@"Extracted colors count %d",result.count);
关于ios - 从图像中获取所有颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21350091/