iOS 13 introduced UIImage instances that auto-adopt to the current UIUserInterfaceStyle (aka light or dark mode). However, there seem to be only methods to construct such images from named or system images (imageNamed:inBundle:withConfiguration: or systemImageNamed:withConfiguration:).
Is there a way to dynamically generate a universal light/dark mode UIImage from Core Graphics (e.g. using two CGImages or using UIGraphicsImageRenderer)?
I don't see any API for that but maybe I'm wrong.
Here's my implementation in Swift 5
This implementation:
displayScaleanduserInterfaceLevel)userInterfaceLevelorhorizontalSizeClass, usage of the dark image will be unaffected and still used if and only if the interface style is dark)Example 1
Assume we have two variants already loaded:
Example 2
Assume we want a red image, dynamic for light/dark, simply call:
where
generatefunction is as follows:The result: