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

时间:2018-12-15 00:16:14

标签: ios swift 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!
}

两者都不起作用。

1 个答案:

答案 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!

    }