问题
I want my UI to support dark mode, but not my PKCanvasView. I used
myCanvasView.overrideUserInterfaceStyle = .light
so the dark mode does not apply to it and it worked: Black lines stay black and white lines stay white, while the rest of the UI is responsive to the system setting.
However, when I try to capture an image of the PKDrawing like this:
myCanvasView.drawing.image(from: myCanvasView.bounds, scale: CGFloat(1.0))
the white lines on the image change to black and black lines change to white when dark mode is active. I set overrideUserInterfaceStyle to .light on all of my ViewControllers, but the image of the drawing is still affected by the system setting. Only when overriding the User Interface Stile in the info.plist file, the image of the PKDrawing stays like I want it to be, even when I set overrideUserInterfaceStyle on all ViewControllers to .dark
Is there a way to get a "light mode" image from PKDrawing without overriding the info.plist file and therefore losing the possibility to respond to the system setting? To me it seems like the image() function of PKDrawing just checks User Interface Style that is set for the app and not for any ViewControllers. Overriding the method won't work since PKDrawing is an opaque object.
EDIT: My idea would be to override the interface style for the app in info.plist and then check the system setting and set the Interface Style for all ViewControllers according to the setting. This would make the images of the drawing normal like they should be, but keeps the responsiveness to the system setting. However, I did not find any information on how to check if the dark mode is enabled in system settings or not.
回答1:
I am not sure how I figured it out, but the solution is quite simple. Generating the image like this:
self.traitCollection.performAsCurrent {
drawingImage = myCanvasView.drawing.image(from: myCanvasView.bounds, scale: CGFloat(1.0))
}
Ignores the system dark mode setting and keeps the drawing as it is.
来源:https://stackoverflow.com/questions/58501913/get-image-of-pkdrawing-in-light-mode