iOS:将RGB滤镜应用于灰度PNG

我有一个灰度的gem顶视图。

(PNG格式,所以有alpha组件)

我想从这个图像创build12个小尺寸的button,每个都有不同的颜色。

为了整洁,我想在代码中做这个,而不是在一些艺术包的外面。

任何人都可以提供一个方法(甚至是一些代码)来做到这一点?

PS我知道如何在GL中使用一个荒谬的数量代码,我希望有一个更简单的方式使用核心graphics/核心animation

编辑:工作解决scheme,感谢从下面的答案精彩

CGSize targetSize = (CGSize){100,100}; UIImage* image; { CGRect rect = (CGRect){ .size = targetSize }; UIGraphicsBeginImageContext( targetSize ); { CGContextRef X = UIGraphicsGetCurrentContext(); UIImage* uiGem = [UIImage imageNamed: @"GemTop_Dull.png"]; // draw gem [uiGem drawInRect: rect]; // overlay a red rectangle CGContextSetBlendMode( X, kCGBlendModeColor ) ; CGContextSetRGBFillColor ( X, 0.9, 0, 0, 1 ); CGContextFillRect ( X, rect ); // redraw gem [uiGem drawInRect: rect blendMode: kCGBlendModeDestinationIn alpha: 1. ]; image = UIGraphicsGetImageFromCurrentImageContext(); } UIGraphicsEndImageContext(); } 

最简单的方法是将图像绘制到RGB颜色空间CGBitmapContext中 ,使用CGContextSetBlendMode设置kCGBlendModeColor ,然后用纯色(例如使用CGContextFillRect )绘制图像。

最好的结果将来自于使用灰度值来索引从最暗的颜色到最亮的颜色的渐变。 不幸的是我不知道核心graphics的具体做法。

这是对问题的回答和@Anomie的实现方面的改进

首先,把它放在你的UIButton类的开始,或者你的视图控制器。 它将从UIColor转换为RGBA值,您将在稍后使用该值。

 typedef enum { R, G, B, A } UIColorComponentIndices; @implementation UIColor (EPPZKit) - (CGFloat)redRGBAValue { return CGColorGetComponents(self.CGColor)[R]; } - (CGFloat)greenRGBAValue { return CGColorGetComponents(self.CGColor)[G]; } - (CGFloat)blueRGBAValue { return CGColorGetComponents(self.CGColor)[B]; } - (CGFloat)alphaRGBAValue { return CGColorGetComponents(self.CGColor)[A]; } @end 

现在,确保在IB中有自定义图像button,并带有灰度图像和正确的框架。 以编程方式创build自定义图像button会更好更容易,因为:

  • 您可以让IB加载图像,而不必手动加载
  • 您可以调整button并在IB中以可视方式查看
  • 您的IB在运行时看起来更像您的应用程序
  • 您不必手动设置框架

假设你有button在IB(靠近底部将支持以编程方式创build),将此方法添加到您的视图控制器或button幼崽类:

 - (UIImage*)customImageColoringFromButton:(UIButton*)customImageButton fromColor:(UIColor*)color { UIImage *customImage = [customImageButton.imageView.image copy]; UIGraphicsBeginImageContext(customImageButton.imageView.frame.size); { CGContextRef X = UIGraphicsGetCurrentContext(); [customImage drawInRect: customImageButton.imageView.frame]; // Overlay a colored rectangle CGContextSetBlendMode( X, kCGBlendModeColor) ; CGContextSetRGBFillColor ( X, color.redRGBAValue, color.greenRGBAValue, color.blueRGBAValue, color.alphaRGBAValue); CGContextFillRect ( X, customImageButton.imageView.frame); // Redraw [customImage drawInRect:customImageButton.imageView.frame blendMode: kCGBlendModeDestinationIn alpha: 1.0]; customImage = UIGraphicsGetImageFromCurrentImageContext(); } UIGraphicsEndImageContext(); return customImage; } 

然后,您需要在视图控制器或button子类中的设置方法中调用它,并将button的imageView设置为:

 [myButton.imageView setImage:[self customImageColoringFromButton:myButton fromColor:desiredColor]]; 

如果您没有使用IB创buildbutton,请使用以下方法:

 - (UIImage*)customImageColoringFromImage:(UIImage*)image fromColor:(UIColor*)color fromFrame:(CGRect)frame { UIImage *customImage = [image copy]; UIGraphicsBeginImageContext(frame.size); { CGContextRef X = UIGraphicsGetCurrentContext(); [customImage drawInRect: frame]; // Overlay a colored rectangle CGContextSetBlendMode( X, kCGBlendModeColor) ; CGContextSetRGBFillColor ( X, color.redRGBAValue, color.greenRGBAValue, color.blueRGBAValue, color.alphaRGBAValue); CGContextFillRect ( X, frame); // Redraw [customImage drawInRect:frame blendMode: kCGBlendModeDestinationIn alpha: 1.0]; customImage = UIGraphicsGetImageFromCurrentImageContext(); } UIGraphicsEndImageContext(); return customImage; } 

并用以下方式调用它:

 [self.disclosureButton.imageView setImage:[self customImageColoringFromImage:[UIImage imageNamed:@"GemTop_Dull.png"] fromColor:desiredColor fromFrame:desiredFrame]];