These are current view capture steps:
- Add template View to NSHostingController
- Add it to a custom NSWindow
- Save as image with
cacheDisplay(in:to:)
However, using cacheDisplay produces some side-effects and the resulting image does not exactly match View as displayed.
To get around some display issues, we tell SwiftUI to first flatten view hierarchy by itself using the drawingGroup modifier.
But even this has some weirdness, especially with blend modes. First of all, it loses all transparency, but that part is at least consistent with previews/actual rendering after using drawingGroup.
Using .overlay(Color.mint.blendMode(.hue)):

We can get around that by masking the result with the initial view:
|
@ViewBuilder public func hue(_ color: Color) -> some View { |
But this still leaves us with incorrectly blended color in some parts.

Compare to how this looks in previews and when displayed in a window.

These are current view capture steps:
cacheDisplay(in:to:)However, using
cacheDisplayproduces some side-effects and the resulting image does not exactly match View as displayed.To get around some display issues, we tell SwiftUI to first flatten view hierarchy by itself using the drawingGroup modifier.
But even this has some weirdness, especially with blend modes. First of all, it loses all transparency, but that part is at least consistent with previews/actual rendering after using
drawingGroup.Using
.overlay(Color.mint.blendMode(.hue)):We can get around that by masking the result with the initial view:
NewIcon/SampleTemplates/NewHue.swift
Line 53 in f441f60
But this still leaves us with incorrectly blended color in some parts.
Compare to how this looks in previews and when displayed in a window.