我有一个可以像手指画图应用程序一样绘制的UIView,但是有时它是不可见的。我希望能够在它不可见时对其进行截图。另外,我想要它可见的屏幕截图,但是我不想要任何子视图。我只想要UIView本身。这是我尝试过的东西:
func shapshot() -> UIImage? {
UIGraphicsBeginImageContext(self.frame.size)
guard let context = UIGraphicsGetCurrentContext() else {
return nil
}
self.layer.render(in: context)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if image == nil {
return nil
}
// if !SettingsManager.shared.isColorInverted {
//return image!.invertedImage()
// }
return image
}
func snapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, self.isOpaque, UIScreen.main.scale)
layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
两者都不起作用。
答案 0 :(得分:-1)
尝试一下,
func snapshot() -> UIImage {
let bounds = UIScreen.main.bounds
UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
self.view.drawHierarchy(in: bounds, afterScreenUpdates: true)
self.btnShare.isHidden = true
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}