如何在Swift中创建IOSurface

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

1个回答

3
我之前错将像素格式字节顺序颠倒了。正确顺序如下:
for c in macPixelFormatString.utf8.reversed()

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


1
感谢您回答自己的问题!由于您正在使用常量ARGB(或BGRA),请注意,您可以直接使用常量kCVPixelFormatType_32BGRA来避免手动构造像素格式UInt32。正如您通过反转字符串所发现的那样,kCVPixelFormatType_32BGRAkCVPixelFormatType_32ARGB得到了更广泛的支持。 - Liam Don

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接