我正在尝试不同的事情,但还没有找到这个问题的答案。有没有办法给 XIB 文件中 UIImageView 定义的图像着色?
谢谢
这行得通
- (UIImage *)imageWithBurnTint:(UIColor *)color
{
UIImage *img = self;
// 让图标着色 - 假设您的图标是黑色的
UIGraphicsBeginImageContextWithOptions(img.size, NO, 0.0);
CGContextRef 上下文 = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, img.size.height);
CGContextScaleCTM(上下文, 1.0, -1.0);
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
// 绘制 alpha 掩码
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextDrawImage(context, rect, img.CGImage);
// 绘制 tint 颜色,保留原始图像的 alpha 值
CGContextSetBlendMode(context, kCGBlendModeSourceIn);
[颜色设置填充];
CGContextFillRect(context, rect);
UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
返回彩色图像;
}
接着
[anImageViewInXib setImage:[center.image imageWithBurnTint:[UIColor blackColor]]];
您需要做的就是更改系统的按钮类型

然后您将能够管理图像的色调
结果如下图所示:

您可以使用新的 iOS7 api 来执行此操作。
UIImage *image = [self.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imageView.image = image;
imageView.tintColor = [UIColor redColor];
第一步是获取现有图像并复制该图像,并将渲染模式设置为模板。将返回的图像设置回 imageView,最后在图像视图上设置色调颜色。
你可以试试这个。用于色调图像
@interface UIImage (Additions)
+ (UIImage *)imageWithColor:(UIColor *)color;
#import "UIImage+Additions.h"
@implementation UIImage (Additions)
+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}