【发布时间】:2023-04-03 10:17:02
【问题描述】:
我有一个代码,其中根据条件是否为真,翻转图像并更改其 tintColor。
func incoming(incoming: Bool) {
let image = UIImage(named: "MessageBubble")?.imageWithRenderingMode(.AlwaysTemplate)
var shownImage: UIImage
// Check whether it is an incoming or outgoing message
if incoming {
let flippedImage = UIImage(CGImage: image!.CGImage!, scale: image!.scale, orientation: UIImageOrientation.UpMirrored)
shownImage = flippedImage.imageWithRenderingMode(.AlwaysTemplate)
bubbleImageView.tintColor = UIColor(red: 100/255, green: 200/255, blue: 100/255, alpha: 1)
} else {
shownImage = image!
bubbleImageView.tintColor = UIColor(red: 100/255, green: 255/255, blue: 255/255, alpha: 1)
}
bubbleImageView.image = shownImage
}
但是,此代码不会翻转图像,但会正确应用 tintColor。如果删除了“.imageWithRenderingMode(.AlwaysTemplate)”函数,则图像正确翻转但不应用 tintColor。 (bubbleImageView 是一个 UIImageView)。
else 块下的代码以正确的 tintColor 显示。
如何让图像翻转并改变颜色?
【问题讨论】:
标签: ios swift uiimageview uiimage