首页 > 解决方案 > 为什么 CIContext.init() 不应该返回 nil?

问题描述

我真的很困惑,因为我遇到了构造函数在根据 API 定义不应该CIContext返回的情况。nil

我有这个代码:

if (context == nil) {
    print("creating default context")
    context = CIContext()
    print("default context is nil: ", context == nil)
}

if (context == nil) {
    print("creating context 1 with options")
    context = CIContext(options: [.allowLowPower: true])
    print("context is nil: ", context == nil)
}

if (context == nil) {
    print("creating context 2 with options")
    context = CIContext(options: [.useSoftwareRenderer: true])
    print("context is nil: ", context == nil)
}

它给了我这个输出:

creating default context
default context is nil:  true

creating context 1 with options
context is nil:  true

creating context 2 with options
context is nil:  false

即前两个块初始化context为零。当 API 说它不应该为 nil 时,这怎么可能呢?

顺便说一句:这仅发生在 CI 机器上,这些机器是 7 岁的 Mac Pro,可能没有连接显示器。

标签: swiftcore-graphics

解决方案


推荐阅读