auramagi / NewIcon

macOS icon customization
MIT License
13 stars 0 forks source link

Taking View capture with cacheDisplay is buggy #6

Open auramagi opened 2 years ago

auramagi commented 2 years ago

These are current view capture steps:

  1. Add template View to NSHostingController
  2. Add it to a custom NSWindow
  3. 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: https://github.com/auramagi/NewIcon/blob/f441f60ac09beb74ec46719aff0ef1277e8ae6ef/SampleTemplates/NewHue.swift#L53

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.

auramagi commented 2 years ago

iOS 16 / macOS Ventura brings the new ImageRenderer class 🎉

Need to test whether it solves these problems.

auramagi commented 2 years ago

ImageRenderer does seem to eliminate blend mode problems, but so does cacheDisplay in Ventura 🤔

cacheDisplay ImageRenderer

ImageRenderer output seems to be blurry, but maybe I'm just holding it wrong. Will revisit later, for now experimental support is in feature/ImageRenderer branch.