【问题标题】:snapshotView(afterScreenUpdates:true) returns empty view when run on real devicesnapshotView(afterScreenUpdates:true) 在真实设备上运行时返回空视图
【发布时间】:2019-11-12 11:04:15
【问题描述】:

在模拟器(iPhone 7 和 iPhone XR)中运行时,snapshotView(afterScreenUpdates: true) 运行良好且符合预期。但是,当我在我的物理 iPhone 7 设备上对其进行测试时,它会返回一个空白视图但具有正确的框架

我需要 UIView 对象,并且不能像以前对类似问题的许多答案所建议的那样使用 UIImage。

let snappedView = view.snapshotView(afterScreenUpdates: true)

【问题讨论】:

  • 你能解决这个问题吗?

标签: ios swift iphone ios-simulator


【解决方案1】:

如果需要,您可以使用此功能截取 iPhone 屏幕截图:

func takeSnapshot() {
  UIGraphicsBeginImageContext(view.frame.size)
  view.layer.render(in: UIGraphicsGetCurrentContext()!)
  let img = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
}

【讨论】:

    【解决方案2】:

    也许这个扩展对你有用:

    public extension UIView {
    
        public func snapshotImage() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
            drawHierarchy(in: bounds, afterScreenUpdates: false)
            let snapshotImage =         UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return snapshotImage
        }
    
        public func snapshotView() -> UIView? {
            if let snapshotImage = snapshotImage() {
                return UIImageView(image: snapshotImage)
            } else {
                return nil
            }
        }
    }
    

    【讨论】:

    • 这没问题,但缺乏 snapshotView(afterScreenUpdates:) 的性能,所以对我的目的没有用
    猜你喜欢
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多