// 开启上下文
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, true, 0)
// 渲染
self.view.drawHierarchy(in: self.view.bounds, afterScreenUpdates: true)
let result = UIGraphicsGetImageFromCurrentImageContext()
// 关闭上下文
UIGraphicsEndImageContext()
resultImgView.image = result
// 开启上下文
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, true, 0)
guard let context = UIGraphicsGetCurrentContext() else {
return
}
self.view.layer.render(in: context)
guard let cgImg = context.makeImage() else {
return
}
// 关闭上下文
UIGraphicsEndImageContext()
resultImgView.image = UIImage(cgImage: cgImg)
网友评论