'How to take a screenshot of uiview always in light mode
Hi I use this function to take a screenshot of a view
func takeScreenshot() -> UIImage {
// Begin context
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)
// Draw view in that context
drawHierarchy(in: self.bounds, afterScreenUpdates: true)
// And finally, get image
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if (image != nil)
{
return image!
}
return UIImage()
}
The problem is that, if I'm in dark mode, text is white and when I print the image, that I taken, text not appears because is white. Can I take a screenshot in light mode also when I in dark mode?
Solution 1:[1]
Set overrideUserInterfaceStyle to .light to force light mode for the view
var screenshot: UIImage {
self.overrideUserInterfaceStyle = .light
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
drawHierarchy(in: bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image ?? UIImage()
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hasan Kassem |
