【问题标题】:Is it possible to capture a UIImage screenshot of a UIReplicantView?是否可以捕获 UIReplicantView 的 UIImage 屏幕截图?
【发布时间】:2016-07-21 01:34:09
【问题描述】:

a lot of ways 可以截屏UIView。捕获UIViewUIView“screengrab”的实际方法是snapshotViewAfterScreenUpdates,它返回UIReplicantViewUIView 的子类)。

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

【问题讨论】:

    标签: uiview uiimage uikit core-graphics calayer


    【解决方案1】:

    这是我们用来将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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      • 2014-01-03
      • 2018-04-21
      • 2023-03-31
      • 2011-10-13
      相关资源
      最近更新 更多