【问题标题】:How to create an IOSurface in Swift如何在 Swift 中创建 IOSurface
【发布时间】:2019-02-24 13:59:31
【问题描述】:

我正在尝试在 Swift 中创建一个 IOSurface,然后从中创建一个 CIImage。 IOSurfaceRef 看起来不错,但 CIImage 返回 nil:

    textureImageWidth = 1024
    textureImageHeight = 1024

    let macPixelFormatString = "ARGB"
    var macPixelFormat: UInt32 = 0
    for c in macPixelFormatString.utf8 {
        macPixelFormat *= 256
        macPixelFormat += UInt32(c)
    }

    let ioSurface = IOSurfaceCreate([kIOSurfaceWidth: textureImageWidth,
                     kIOSurfaceHeight: textureImageHeight,
                     kIOSurfaceBytesPerElement: 4,
                     kIOSurfaceBytesPerRow: textureImageWidth * 4,
                     kIOSurfaceAllocSize: textureImageWidth * textureImageHeight * 4,
                     kIOSurfacePixelFormat: macPixelFormat] as CFDictionary)!

    IOSurfaceLock(ioSurface, IOSurfaceLockOptions.readOnly, nil)
    let test = CIImage(ioSurface: ioSurface)
    IOSurfaceUnlock(ioSurface, IOSurfaceLockOptions.readOnly, nil)

有什么建议吗?我猜 CIImage 初始化程序需要更多元数据来完成它的工作,但我不知道是什么。

【问题讨论】:

    标签: ios swift core-image ciimage iosurface


    【解决方案1】:

    我的像素格式字节顺序错误。应该是:

    for c in macPixelFormatString.utf8.reversed()
    

    留下这个问题,因为我认为在 stackoverflow 上没有任何其他在 Swift 中使用 IOSurfaceCreate 的示例。

    【讨论】:

    • 感谢您回来回答您自己的问题!由于您使用的是常量 ARGB(或 BGRA),请注意您可以只使用常量 kCVPixelFormatType_32BGRA 来避免手动构建像素格式 UInt32。正如您通过反转字符串发现的那样,kCVPixelFormatType_32BGRAkCVPixelFormatType_32ARGB 得到更广泛的支持。
    猜你喜欢
    • 1970-01-01
    • 2015-10-27
    • 2014-07-24
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 2018-02-22
    相关资源
    最近更新 更多