【问题标题】:Swift restore original image color after casting a tint投射色调后迅速恢复原始图像颜色
【发布时间】:2017-02-12 11:28:19
【问题描述】:
我在UIImageView 中有一张图片,它带有默认颜色,比如说自定义灰色。
在我的代码中,我以这种方式设置了不同的tint
myImageView.image = myImageView.image!.withRenderingMode(.alwaysTemplate)
myImageView.tintColor = someCondition() ? UIColor.red : UIColor.lightGray
事实是我无法恢复图像的原始灰色。我的问题是,是否有办法将我的代码中的 UIColor.lightGray 部分替换为告诉 UIImageView not 使用任何色调,但使用其原始颜色的内容。
【问题讨论】:
标签:
ios
swift
uiimageview
【解决方案1】:
要不应用任何色调,您可以像这样设置图像渲染模式:
image.renderingMode = .alwaysOriginal
在您的代码中,您可以说:
let renderingMode: UIImageRenderingMode = condition ? .alwaysTemplate : .alwaysOriginal
myImageView.image = myImageView.image?.withRenderingMode(renderingMode)
【解决方案2】:
我正在使用这种方法来更改/删除点击时的色调
var imageView:UIImageView = {
let img = UIImageView()
img.image = someImage.withRenderingMode(.alwaysTemplate)
img.tintColor = .cyan
return img
}()
将手势识别器添加到带有动作的图像视图或按钮,然后只分配已经分配给图像视图但具有不同渲染模式的图像
@objc func imgTap(){
imageView.image! = imageView.image!.withRenderingMode(.alwaysOriginal)
}