如何在SwiftUI中设置图像色调?

如何在SwiftUI中设置图像色调?,swiftui,Swiftui,我正在尝试在SwiftUIimage类中设置图像色调 对于UIKit,我可以使用 let image = UIImage(systemName: "cart")!.withTintColor(.blue) 但是我在swiftui文档中找不到这样的设置在newswiftui上对于settint使用: Image("ImageName") .foregroundColor(.red) 根据源图像的不同,您可能还需要一个附加的渲染模式修改器: Image("Ima

我正在尝试在SwiftUI
image
类中设置图像色调

对于UIKit,我可以使用

let image = UIImage(systemName: "cart")!.withTintColor(.blue)

但是我在swiftui文档中找不到这样的设置

在new
swiftui
上对于set
tint
使用:

Image("ImageName")
  .foregroundColor(.red)

根据源图像的不同,您可能还需要一个附加的渲染模式修改器:

Image("ImageName")
  .renderingMode(.template)
  .colorMultiply(.red)
// or
Image("ImageName")
  .colorMultiply(.blue)

您可以阅读这篇文章。

上面的答案可能是最好的答案,尽管这也很有效:

let uiImage = UIImage(systemName: "cart")!.withTintColor(.blue)
let image = Image(uiImage: uiImage)
然后,您可以将名为
image
的常量与SwiftUI一起使用。

这对我很有用

Image("ImageName")
   .renderingMode(.template)
   .foregroundColor(.white)

您可能需要全局声明
uiImage
。我就是这样做的,它对我很有用。根据源图像的不同,您可能还需要一个额外的渲染模式修改器:
image(“ImageName”).renderingMode(.template”).colorMultiply(.red)
此解决方案的问题在于,取决于您处于亮模式还是暗模式,模板图像可能以黑色和黑色开始。colorMultiply将没有。colorMultiply根据图像的原始颜色给出不同的结果。这应该是可接受的答案,因为它在明暗模式下都有效。这是唯一对我有效的答案