我正在尝试在 SwiftUIImage类中设置图像色调
对于 UIKit,我可以使用设置图像色调
let image = UIImage(systemName: "cart")!.withTintColor(.blue)
Run Code Online (Sandbox Code Playgroud)
但我在 swiftui 文档中找不到这样的设置
atu*_*n23 47
新swiftUI套装tint使用:
Image("ImageName")
.foregroundColor(.red)
Run Code Online (Sandbox Code Playgroud)
根据源图像,您可能还需要额外的渲染模式修改器:
Image("ImageName")
.renderingMode(.template)
.colorMultiply(.red)
// or
Image("ImageName")
.colorMultiply(.blue)
Run Code Online (Sandbox Code Playgroud)
您可以阅读此主题。
Mon*_*ona 38
这对我有用,
Image("ImageName")
.renderingMode(.template)
.foregroundColor(.white)
Run Code Online (Sandbox Code Playgroud)
ARG*_*Geo 12
对于着色,您可以使用.blendMode修改器将图像与其他图像或颜色合成。Xcode 14.0+ 中有 20 多种类似 Photoshop 的混合模式。最适合 SwiftUI 着色的混合模式是:
如果更改混合颜色的亮度和饱和度,您将得到与此处看到的不同的结果。
这是代码:
struct ContentView: View {
var body: some View {
ZStack {
Rectangle()
.fill(Gradient(colors: [.red, .yellow]))
.ignoresSafeArea()
Image("XcodeIcon")
.resizable()
.scaledToFit()
.blendMode(.softLight)
}
}
}
Run Code Online (Sandbox Code Playgroud)
SwiftUI 4.x:
方法一:
Image("your_image_name")
.resizable()
.renderingMode(.template)
.foregroundColor(.white)
Run Code Online (Sandbox Code Playgroud)
方法2:
Image("your_image_name")
.resizable()
.renderingMode(.template)
.colorMultiply(.red)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8486 次 |
| 最近记录: |