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

10-10 21:03