如何以编程方式动态地为UIButton着色背景图像?

我正在开发一个应用程序 – 或者更确切地说是一些可重用的“框架”,我很乐意在它工作时分享它。 在此应用程序中,用户应该能够从颜色主题列表中进行选择。 因此,应用程序必须能够以某种相当动态的方式对其UI元素进行着色。

对于按钮,所有着色都不起作用。 必须在此处提供正确着色的背景图像。 但是为每个人准备一套背景图像只是第二好的。 它不够动态和灵活。

最后,解决方案可能归结为为所选和正常状态提供一个单色(灰色)梯度图像,并使用CoreGraphics或OpenGL以编程方式对图像着色。 但坦率地说,我不知道从哪里开始。 渐变应该如何,然后我将如何以任何给定的颜色以编程方式着色?

几乎适用于UISegmentedControls,只是有点复杂。 :)任何涵盖UISegementedControls的通用解决方案也非常受欢迎。

我在另一篇post后面创建了一个UIImage类别,我发现它无法获取图像并将其调整如下:

- (UIImage *)tintedImageWithColor:(UIColor *)tintColor { UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 0, self.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); // draw alpha-mask CGContextSetBlendMode(context, kCGBlendModeNormal); CGContextDrawImage(context, rect, self.CGImage); // draw tint color, preserving alpha values of original image CGContextSetBlendMode(context, kCGBlendModeSourceIn); [tintColor setFill]; CGContextFillRect(context, rect); UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return coloredImage; } 

这将拍摄图像并使用给定颜色的alpha值填充所有区域。

在iOS7中,您可以使用方法imagedWithRenderingMode:与方法setTintColor:结合使用setTintColor:

但是,请务必使用UIImageRenderingModeAlwaysTemplate因为它会使用UIImageRenderingModeAlwaysTemplate替换UIImage上的所有非透明颜色。 默认为UIImageRenderingModeAutomatic

UIImageRenderingModeAlwaysTemplate

 Always draw the image as a template image, ignoring its color information. 

例:

  [myButton setTintColor:[UIColor colorWithRed:0 green:0.8196f blue:0.0039f alpha:1]]; UIImage * image = [myButton.currentBackgroundImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [myButton setBackgroundImage:image forState:UIControlStateNormal]; 

我使用horsejockey的代码创建了一个UIButton类别: https : //github.com/filipstefansson/UITintedButton

这是新方法:

 -(void)setImageTintColor:(UIColor *)color; -(void)setBackgroundTintColor:(UIColor *)color forState:(UIControlState)state; +(void)tintButtonImages:(NSArray *)buttons withColor:(UIColor *)color; +(void)tintButtonBackgrounds:(NSArray *)buttons withColor:(UIColor *)color forState:(UIControlState)state; 

以下是对您的问题的部分答案。 这是我写的一个帮助方法,它为灰度图像着色:

 // baseImage is the grey scale image. color is the desired tint color + (UIImage *)tintImage:(UIImage *)baseImage withColor:(UIColor *)color { UIGraphicsBeginImageContextWithOptions(baseImage.size, NO, baseImage.scale); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height); CGContextScaleCTM(ctx, 1, -1); CGContextTranslateCTM(ctx, 0, -area.size.height); CGContextSaveGState(ctx); CGContextClipToMask(ctx, area, baseImage.CGImage); [color set]; CGContextFillRect(ctx, area); CGContextRestoreGState(ctx); CGContextSetBlendMode(ctx, kCGBlendModeOverlay); CGContextDrawImage(ctx, area, baseImage.CGImage); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // If the original image was stretchable, make the new image stretchable if (baseImage.leftCapWidth || baseImage.topCapHeight) { newImage = [newImage stretchableImageWithLeftCapWidth:baseImage.leftCapWidth topCapHeight:baseImage.topCapHeight]; } return newImage; } 

我正在向你推荐一个似乎正在寻找的问题而且它得到了答案。

如何在iPhone中为透明的PNG图像着色?

祝你好运!

只是你问题标题上的一个小细节…你的意思是“如何”而不是“热”对吗?

我修改了“horsejockey”中的一些东西:

 +(UIImage *)getTintedImage:(UIImage*)image withColor:(UIColor *)tintColor { UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 0, image.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); // draw alpha-mask CGContextSetBlendMode(context, kCGBlendModeNormal); CGContextDrawImage(context, rect, image.CGImage); // draw tint color, preserving alpha values of original image CGContextSetBlendMode(context, kCGBlendModeSourceIn); [tintColor setFill]; CGContextFillRect(context, rect); UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return coloredImage; }