【问题标题】:drawHierarchy fails when afterScreenUpdates: true当 afterScreenUpdates: true 时 drawHierarchy 失败
【发布时间】:2016-11-04 17:51:37
【问题描述】:

我正在使用以下代码来截取我的视图。

UIGraphicsBeginImageContext(self.view.bounds.size)
self.view.drawHierarchy(in: self.view.bounds, afterScreenUpdates: true)
let wholeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

如果我将“afterScreenUpdates:”设置为 false,它可以正常工作。但如果我将其设置为 true,则会出现以下错误:

*** Assertion failure in -[UIApplication _performWithUICACommitStateSnapshotting:](), /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.5.2/UIApplication.m:6882

使用断点,我发现错误是在 drawHierarchy 方法上抛出的。以前有人见过这个错误吗?知道发生了什么吗?在获取快照之前,我尝试对视图进行任何更新(隐藏一些 uiimages),但它没有效果。

奇怪的附注:应用程序因此错误而冻结,但没有硬停止(我无法与调试器交互以查看回溯)。抱歉,如果不清楚。

【问题讨论】:

    标签: ios swift uigraphicscontext


    【解决方案1】:

    我也遇到了这个错误。问题是 iOS 中的所有 UIKit 工作都需要在主线程上完成。一个快速的解决方案是将其包装在来自主线程的 DispatchQueue.async 调用中:

    var wholeImage : UIImage?
    
    DispatchQueue.main.async {
        UIGraphicsBeginImageContext(self.view.bounds.size)
        self.view.drawHierarchy(in: self.view.bounds, afterScreenUpdates: true)
        self.wholeImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多