有a lot of ways屏幕抓取UIView
。捕获UIView
的{{1}}“screengrab”的事实上的方式是UIView
,它返回snapshotViewAfterScreenUpdates
(UIReplicantView
的子类)。
有没有办法从这个看似顽固的类中创建UIView
?
答案 0 :(得分:0)
这是我们用于将UIView
捕获到UIImageView
的扩展程序。该解决方案不包含您建议的UIReplicatView
的任何用法。但它对我们来说非常有用:
extension UIView {
// Note: only return the image after viewDidAppear called
func takeScreenShot() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, UIScreen.main.scale)
if let scrollView = self as? UIScrollView {
let offset = scrollView.contentOffset
UIGraphicsGetCurrentContext()?.translateBy(x: -offset.x, y: -offset.y);
}
drawHierarchy(in: bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}