ios - 从图像中获取所有颜色

标签 ios objective-c image-processing uiimage cifilter

我有一个带有 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/

相关文章:

.net - 为 .net 商店开始移动/IOS 开发的最佳方式

image - 如何计算 Gabor 滤波器中的空间频率?

ios - 我们如何确保第 3 方库不会在我的 iOS 应用程序中发送用户数据?

ios - 在 Swift 中使用 WillSpeakRange 委托(delegate)中断 AVSpeechUtterance 和恢复

iphone - 检测iphone 5 4"屏幕

ios - 选择索引处的对象时执行EXC BAD指令

image - 如何在 drupal 8 中使用 ckeditor 调整上传的内联图像的大小

python - 将像素从图像的裁剪部分转换为原始图像 OpenCV

iphone - 为什么 NSNumber 类不能正确地将我的 NSString 对象转换为 long long?

ios - 在 iOS 中搜索数组字典数组(某些模型)