2

我正在尝试在 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 初始化器需要更多的元数据来完成它的工作,但我不知道是什么。

4

1 回答 1

2

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

for c in macPixelFormatString.utf8.reversed()

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

于 2018-09-20T13:49:39.137 回答