How can I create a SwiftUI View
extension to return a NSImage
? I have seen how it can be done on iOS using UIGraphicsImageRenderer
but it seems there is no macOS equivalent.
Convert SwiftUI View to NSImage
View+Image.swift
import SwiftUI
extension View {
func renderAsImage() -> NSImage? {
let view = NoInsetHostingView(rootView: self)
view.setFrameSize(view.fittingSize)
return view.bitmapImage()
}
}
NoInsetHostingView.swift
import SwiftUI
class NoInsetHostingView<V>: NSHostingView<V> where V: View {
override var safeAreaInsets: NSEdgeInsets {
return .init()
}
}
NSView+Image.swift
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)
}
}
I'm not sure exactly why NoInsetHostingView
is needed, but with just a normal NSHostingView
the image has undesired insets, and this fixes it.
Enormous gratitude https://mcmap.net/q/1163256/-creating-an-skspritenode-from-the-sf-symbols-font-in-a-different-color –
Outbreak
© 2022 - 2024 — McMap. All rights reserved.