问题描述
如何通过编程更改UIImage的颜色,请帮忙?如果我发送UIImage,它的颜色需要更改任何帮助吗?如果我通过位图处理更改RGB颜色,则它将不起作用.
How can I change the UIImage's color through programming, any help please? If I send a UIImage, its color needs to change any help please? If I change the RGB color through bitmaphandling, it does not work.
推荐答案
如果只需要让它与众不同,只需使用imageView.tintColor
(iOS 7+).抓住的是,设置tintColor
在默认情况下不执行任何操作:
If you only need it to look different, just use imageView.tintColor
(iOS 7+). Catch is, setting tintColor
doesn't do anything by default:
要使其正常运行,请使用imageWithRenderingMode:
To make it work, use imageWithRenderingMode:
var image = UIImage(named: "stackoverflow")!
image = image.imageWithRenderingMode(.AlwaysTemplate)
let imageView = ...
imageView.tintColor = UIColor(red: 0.35, green: 0.85, blue: 0.91, alpha: 1)
imageView.image = image
现在它将起作用:
性能
在配置UIImageView
之后设置图像可以避免重复昂贵的操作:
Setting the image after configuring the UIImageView
avoids repeating expensive operations:
// Good usage
let imageView = ...
imageView.tintColor = yourTintColor
var image = UIImage(named: "stackoverflow")!
image = image.imageWithRenderingMode(.AlwaysTemplate)
imageView.image = image // Expensive
// Bad usage
var image = UIImage(named: "stackoverflow")!
image = image.imageWithRenderingMode(.AlwaysTemplate)
let imageView = ...
imageView.image = image // Expensive
imageView.frame = ... // Expensive
imageView.tintColor = yourTint // Expensive
获取&异步设置图像可减少滚动和动画滞后(尤其是在UICollectionViewCell
或UITableViewCell
内部着色图像时):
Getting & setting the image asynchronously reduces scrolling and animation lag (especially when tinting an image inside of a UICollectionViewCell
or UITableViewCell
):
let imageView = cell.yourImageView
imageView.image = nil // Clear out old image
imageView.tintColor = UIColor(red: 0.35, green: 0.85, blue: 0.91, alpha: 1)
// Setting the image asynchronously reduces stuttering
// while scrolling. Remember, the image should be set as
// late as possible to avoid repeating expensive operations
// unnecessarily.
dispatch_async(dispatch_get_main_queue(), { () -> Void in
var image = UIImage(named: "stackoverflow")!
image = image.imageWithRenderingMode(.AlwaysTemplate)
imageView.image = image
})
这篇关于UIImage颜色改变了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!