ios swift UIView 截屏成图片 UIimage

304 阅读1分钟
extension UIView {

    func snapshotImage() -> UIImage? {

        UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, UIScreen.main.scale)

        self.layer.render(in: UIGraphicsGetCurrentContext()!)

        let img = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        return img

    }
 }