You have to set colorBlendFactor
on the sprite.
func makeSprite(symbolName: String) -> SKSpriteNode {
let image = Image(systemName: symbolName)
// Thanks Oskar!
// https://mcmap.net/q/1067996/-convert-swiftui-view-to-nsimage
// See my adapted version of his code further down; I
// think you won't need it if you're on iOS, meaning
// using UIImage, but that's all black magic to me.
let renderedByOskar = image.renderAsImage()!
let texture = SKTexture(image: renderedByOskar)
let sprite = SKSpriteNode(texture: texture)
sprite.color = .green
sprite.colorBlendFactor = 1 // <-- This
return sprite
}
A green symbol, thanks to black magic:
Many thanks to Oskar for the rendering extensions that enable me to do this on macOS, meaning, without UIImage
:
class NoInsetHostingView<V>: NSHostingView<V> where V: View {
override var safeAreaInsets: NSEdgeInsets {
return .init()
}
}
extension Image {
func renderAsImage() -> NSImage? {
let view = NoInsetHostingView(rootView: self)
view.setFrameSize(view.fittingSize)
return view.bitmapImage()
}
}
public extension NSView {
func bitmapImage() -> NSImage? {
guard let rep = bitmapImageRepForCachingDisplay(in: bounds) else {
return nil
}
cacheDisplay(in: bounds, to: rep)
guard let cgImage = rep.cgImage else {
return nil
}
return NSImage(cgImage: cgImage, size: bounds.size)
}
}
withTintColor(.red, renderingMode: .alwaysOriginal)
? – Ji.alwaysTemplate
, the point being whatever mode it's picking for the context of making a texture, try the other... – Ji.alwaysOriginal
and it was the same, I’ll give the other a shot and see if that makes any difference. – Sememe.init
is optional – Nationalityextension
in this answer helped me https://mcmap.net/q/188417/-changing-uiimage-color – Adelleadelpho