我正在尝试更改使用 SwiftUI 画布绘制的图像的前景色。 它使用来自 SFSymbols 的图像,但不适用于资产中的自定义图像。 该图像采用 SVG 格式,并在资产中设置了“模板模式渲染”。 图像已绘制,但保持白色。
// create canvas to draw
Canvas { context, size in
var image = context.resolve(Image("season-drinkfrom")) => not working
//var image = context.resolve(Image(systemName: "star.fill")) => working
image.shading = .color(.green)
let imageRect = CGRect(x: drinkFromOriginX - imageSize/2, y: 0, width: imageSize, height: imageSize)
context.draw(image, in: imageRect)
}
}
资产图像保持白色前景色 - 不起作用:
SFSymbol 图像使用新颜色(绿色)- 工作:
这可能是图像的上下文错误。
有关信息,资产目录中设置的资产图像:
这是我用来测试改变颜色的代码和图像 SVG 格式图像。
图片为“SVG_Logo”,下载地址:https://en.wikipedia.org/wiki/SVG#/media/File:SVG_Logo.svg
并拖入Xcode工程的
Assets.xcassets
注意,我取消勾选
Resizing Preserve Vector Data
,图像变成绿色,否则图像保持不变。
struct ContentView: View {
var body: some View {
Canvas { context, size in
var image = context.resolve(Image("SVG_Logo"))
// var image = context.resolve(Image(systemName: "star.fill"))
image.shading = .color(.green)
let imageRect = CGRect(x: 0, y: 0, width: 222, height: 222)
context.draw(image, in: imageRect)
}
}
}