如何在iOS和WatchKit中更改图像tintColor
本文翻译自:How can I change image tintColor in iOS and WatchKit
I have an UIImageView called "theImageView", with UIImage in a single color (transparent background) just like the left black heart below. 我有一个名为“ theImageView”的UIImageView,UIImage具有单色(透明背景),就像下面的黑色左心一样。 How can I change the tint color of this image programmatically in iOS 7 or above, as per the tint method used in the iOS 7+ Navigation Bar icons? 如何按照iOS 7+导航栏图标中使用的着色方法,在iOS 7或更高版本中以编程方式更改此图像的着色颜色?
Can this method also work in WatchKit for an Apple Watch app? 此方法也可以在Apple Watch应用程序的WatchKit中使用吗?
#1楼
参考:https://stackoom.com/question/1IsFh/如何在iOS和WatchKit中更改图像tintColor
#2楼
Here's a category that should do the trick 这是应该解决的问题
@interface UIImage(Overlay)
@end@implementation UIImage(Overlay)- (UIImage *)imageWithColor:(UIColor *)color1
{UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);CGContextRef context = UIGraphicsGetCurrentContext();CGContextTranslateCTM(context, 0, self.size.height);CGContextScaleCTM(context, 1.0, -1.0);CGContextSetBlendMode(context, kCGBlendModeNormal);CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);CGContextClipToMask(context, rect, self.CGImage);[color1 setFill];CGContextFillRect(context, rect);UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return newImage;
}
@end
so you would do: 所以你会做:
theImageView.image = [theImageView.image imageWithColor:[UIColor redColor]];
#3楼
I had to do this in Swift using an extension
. 我必须在Swift中使用extension
执行此操作。
I thought I'd share how I did it: 我以为我会分享我的做法:
extension UIImage {func imageWithColor(color1: UIColor) -> UIImage {UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)color1.setFill()let context = UIGraphicsGetCurrentContext() as CGContextRefCGContextTranslateCTM(context, 0, self.size.height)CGContextScaleCTM(context, 1.0, -1.0);CGContextSetBlendMode(context, CGBlendMode.Normal)let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRectCGContextClipToMask(context, rect, self.CGImage)CGContextFillRect(context, rect)let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImageUIGraphicsEndImageContext()return newImage}
}
Usage: 用法:
theImageView.image = theImageView.image.imageWithColor(UIColor.redColor())
Swift 4 斯威夫特4
extension UIImage {func imageWithColor(color1: UIColor) -> UIImage {UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)color1.setFill()let context = UIGraphicsGetCurrentContext()context?.translateBy(x: 0, y: self.size.height)context?.scaleBy(x: 1.0, y: -1.0)context?.setBlendMode(CGBlendMode.normal)let rect = CGRect(origin: .zero, size: CGSize(width: self.size.width, height: self.size.height))context?.clip(to: rect, mask: self.cgImage!)context?.fill(rect)let newImage = UIGraphicsGetImageFromCurrentImageContext()UIGraphicsEndImageContext()return newImage!}
}
Usage: 用法:
theImageView.image = theImageView.image?.imageWithColor(color1: UIColor.red)
#4楼
Try this 尝试这个
http://robots.thoughtbot.com/designing-for-ios-blending-modes http://robots.thoughtbot.com/designing-for-ios-blending-modes
or 要么
- (void)viewDidLoad
{
[super viewDidLoad];UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 50)];
label.numberOfLines = 0;
label.font = [UIFont systemFontOfSize:13];
label.text = @"These checkmarks use the same gray checkmark image with a tintColor applied to the image view";
[self.view addSubview:label];[self _createImageViewAtY:100 color:[UIColor purpleColor]];
}- (void)_createImageViewAtY:(int)y color:(UIColor *)color {
UIImage *image = [[UIImage imageNamed:@"gray checkmark.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect frame = imageView.frame;
frame.origin.x = 100;
frame.origin.y = y;
imageView.frame = frame;if (color)imageView.tintColor = color;[self.view addSubview:imageView];
}
#5楼
iOS iOS版
For an iOS app, in Swift 3 or 4: 对于iOS应用,在Swift 3或4中:
theImageView.image = theImageView.image?.withRenderingMode(.alwaysTemplate)
theImageView.tintColor = UIColor.red
Swift 2: 斯威夫特2:
theImageView.image = theImageView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
theImageView.tintColor = UIColor.redColor()
Meanwhile, the modern Objective-C solution is: 同时,现代的Objective-C解决方案是:
theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[theImageView setTintColor:[UIColor redColor]];
Watchkit Watchkit
In WatchKit for Apple Watch apps, you can set the tint color for a template image . 在Apple Watch应用程序的WatchKit中,您可以设置模板图像的色调颜色 。
- You must add your image to an Asset Catalog in your WatchKit App, and set the image set to be rendered as a Template Image in the Attributes Inspector. 您必须将图像添加到WatchKit App的资产目录中,并在Attributes Inspector中将图像集设置为呈现为模板图像。 Unlike for an iPhone app, you cannot set the template rendering in code in the WatchKit Extension at present. 与iPhone应用程序不同,当前您无法在WatchKit Extension中的代码中设置模板渲染。
- Set that image to be used in your WKInterfaceImage in interface builder for your app 设置要在应用程序的界面生成器中的WKInterfaceImage中使用的图像
- Create an IBOutlet in your WKInterfaceController for the WKInterfaceImage called 'theImage'... 在WKInterfaceController中为名为'theImage'的WKInterfaceImage创建一个IBOutlet ...
To then set the tint color in Swift 3 or 4: 然后在Swift 3或4中设置色调颜色:
theImage.setTintColor(UIColor.red)
Swift 2: 斯威夫特2:
theImage.setTintColor(UIColor.redColor())
To then set the tint color in Objective-C: 然后在Objective-C中设置色调颜色:
[self.theImage setTintColor:[UIColor redColor]];
If you use a template image and do not apply a tint colour, the Global Tint for your WatchKit app will be applied. 如果您使用模板图像并且不应用色调颜色,则将应用WatchKit应用程序的“全局色调”。 If you have not set a Global Tint, theImage
will be tinted light blue by default when used as a template image. 如果尚未设置全局色调,则在用作模板图像时,默认情况下, theImage
将淡蓝色。
#6楼
With Swift 与斯威夫特
let commentImageView = UIImageView(frame: CGRectMake(100, 100, 100, 100))
commentImageView.image = UIImage(named: "myimage.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
commentImageView.tintColor = UIColor.blackColor()
addSubview(commentImageView)
如何在iOS和WatchKit中更改图像tintColor相关推荐
- 谷歌浏览器在新页面打开_如何在Google文档中更改页面方向
谷歌浏览器在新页面打开 Most of the time, using a portrait orientation for document pages makes sense. Occasiona ...
- 在Markdown中更改图像大小
本文翻译自:Changing image size in Markdown I just got started with Markdown. 我刚开始使用Markdown. I love it, b ...
- react js 添加样式_如何在React JS Application中添加图像?
react js 添加样式 Hello! In this article, we will learn how to add images in React JS? I remember when I ...
- 如何在Google Chrome浏览器中更改主页
Google Chrome opens with a "New Tab" page by default, but it's easy to open the browser wi ...
- linux裁剪图片的软件,ImageMagick教程:如何在Linux命令行中剪裁图像
问题:我想要去除图像文件中的白色空白,有没有什么便捷的方法能在Linux命令行中对图像文件进行剪裁? 当涉及到在Linux中转换或编辑图像文件时,ImageMagick毫无疑问是最为熟知的一体化软件之 ...
- ios 跨域_如何在iOS和Android中建立跨域通信桥
ios 跨域 I was working on a certain project at work, in which I needed to connect several varying comp ...
- word标尺灰色_如何在Microsoft Word 2013中更改标尺测量单位
word标尺灰色 In Word, you can select one of several units of measurement for the ruler. You may be worki ...
- linux裁剪图片的软件,技术|Linux有问必答——如何在Linux命令行中剪裁图像
问题:我想要去除图像文件中的白色空白,有没有什么便捷的方法能在Linux命令行中对图像文件进行剪裁? 当涉及到在Linux中转换或编辑图像文件时,ImageMagick毫无疑问是最为熟知的一体化软件之 ...
- 如何在ABBYY FineReader 12中添加图像
ABBYY FineReader 12是一款OCR文字识别软件,而且强大的它现在还可使用快速扫描窗口中的快速打开.扫描并保存为图像或任务自动化任务在没有进行预处理和OCR的 ABBYY FineRea ...
最新文章
- ThinkPHP调用连连支付
- 地平线:黎明时分中的云渲染技术
- Font Manager :字体管理及批量安装工具
- 前沿 | 阿里达摩院最牛科技~摄像头ISP处理器,提升夜间识别精准率
- php mkdir用户,PHP mkdir()无写权限的问题解决方法
- 【Spring学习】01
- mysql 左右值算法详解_无限分类左右值算法的常规操作逻辑
- django项目如何连接前端_Django项目中前端序列化参数获取
- Google Java编程风格指南中文版(转)
- ADO.NET Entity Framework 学习(1)
- php之简单的文件管理(基本功能
- 某互联网公司数据分析岗 SQL 笔试题
- Tomcat8安装即配置教程
- 利用层次分析法对熵权法进行修正
- c语言汇率转换代码_原生JS实现汇率转换功能代码实例
- 可行性研究报告计算机,计算机软件设计师:软件可行性研究报告[1]
- 关于CDN加速 CDN加速是什么
- 数据分析报告怎么写(二)
- 【论文速递】-2022-金融研究-系统性金融风险文献综述:现状、发展与展望
- 谐振电路的品质因数(Q值)zz