是否可以捕获UIReplicantView的UIImage截图?

时间:2016-07-21 01:34:10

标签: uiview uiimage uikit core-graphics calayer

a lot of ways屏幕抓取UIView。捕获UIView的{​​{1}}“screengrab”的事实上的方式是UIView,它返回snapshotViewAfterScreenUpdatesUIReplicantView的子类)。

有没有办法从这个看似顽固的类中创建UIView

1 个答案:

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