【发布时间】:2013-09-20 09:31:37
【问题描述】:
我尝试了不同的方法,但还没有找到这个问题的答案。 有没有办法给 XIB 文件中 UIImageView 定义的图像着色?
感谢
【问题讨论】:
-
我认为不可能。
-
问题是什么?
标签: ios uiimageview xib
我尝试了不同的方法,但还没有找到这个问题的答案。 有没有办法给 XIB 文件中 UIImageView 定义的图像着色?
感谢
【问题讨论】:
标签: ios uiimageview xib
这行得通
- (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]]];【讨论】:
您需要做的就是更改系统
的按钮类型然后您将能够管理图像的色调
结果如下图:
【讨论】:
你可以试试这个.for tint image
@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;
}
【讨论】:
您可以使用新的 iOS7 api 来执行此操作。
UIImage *image = [self.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imageView.image = image;
imageView.tintColor = [UIColor redColor];
第一步是获取现有图像并复制该图像并将渲染模式设置为模板。将返回的图像设置回 imageView,最后在图像视图上设置 tint 颜色。
【讨论】:
UIImage上使用-imageWithRenderingMode:——你不需要先把它放在一个图像视图中。
awakeFromNib,该示例可以为他放置在笔尖中的 uiimageivew 中的任何图像着色。