繁体   English   中英

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

[英]UIView to UIImage without subviews and hidden

我有一个可以像手指画图应用程序一样绘制的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!

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM