UIView到UIImage,没有子视图和隐藏



我有一个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!
}

两者都不起作用。

试试这个,

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!
}

最新更新