【问题标题】:Using previewCGImageRepresentation in both Xcode 13 and Xcode 12?在 Xcode 13 和 Xcode 12 中使用 previewCGImageRepresentation?
【发布时间】:2021-10-09 03:49:28
【问题描述】:

我遇到了 Xcode 13b2 (iOS 15 SDK) 更改了 AVCapturePhoto.previewCGImageRepresentation() 的 Swift 返回类型的问题。在 Xcode 12.5.1 (iOS 14 SDK) 中,此方法返回 Unmanged<CGImage>。在 13b2 - 13b4 中,返回CGImage?

我需要在两个 Xcode 版本下编译我的代码,因为 Xcode 13 有其他问题,不能用于将构建提交到 App Store。我以为我写这个很聪明,但它不会编译,因为它不是条件代码编译检查,而是运行时检查:

extension AVCapturePhoto {
    func stupidOSChangePreviewCGImageRepresentation() -> CGImage? {
        if #available(iOS 15, *) {
            return self.previewCGImageRepresentation()
        } else {
            return self.previewCGImageRepresentation()?.takeUnretainedValue()
        }
    }
}

另一种可能是创建用户定义的 Xcode 设置,但我认为这不能根据 Xcode 或 SDK 版本有条件地完成。

可能会有一些不安全的指针表演可以做......

还有其他想法吗?

【问题讨论】:

    标签: swift ios15 xcode13


    【解决方案1】:

    您可以检查 swift 编译器的版本。在撰写本文时,维基百科上提供了详尽的列表。

    https://en.wikipedia.org/wiki/Xcode#Toolchain_versions

    extension AVCapturePhoto {
        func stupidOSChangePreviewCGImageRepresentation() -> CGImage? {
        #if compiler(>=5.5)
            return self.previewCGImageRepresentation()
        #else
            return self.previewCGImageRepresentation()?.takeUnretainedValue()
        #endif
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 2020-11-11
      • 2022-11-11
      • 1970-01-01
      • 2022-11-10
      相关资源
      最近更新 更多