一尘不染

如何将CVPixelBufferGetBaseAddress调用转换为Swift?

swift

也许我是Swift中第一个这样做的人,但在Swift中使用&/ inout和uint8_t似乎在网上没有任何东西。有人可以翻译一下吗?这种关系是按位的吗?

目标C

uint8_t *buf=(uint8_t *) CVPixelBufferGetBaseAddress(cvimgRef);

快速尝试

let inout buf:uint8_t = SOMETHING HERE CVPixelBufferGetBaseAddress(cvimgRef)

阅读 513

收藏
2020-07-07

共1个答案

一尘不染

CVPixelBufferGetBaseAddress()返回一个UnsafeMutablePointer<Void>,可以通过将其转换为UInt8指针

let buf = UnsafeMutablePointer<UInt8>(CVPixelBufferGetBaseAddress(pixelBuffer))

Swift 3(Xcode 8)的更新,Swift 5(Xcode 11)的检查:

if let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer) {
    let buf = baseAddress.assumingMemoryBound(to: UInt8.self)
    // `buf` is `UnsafeMutablePointer<UInt8>`
} else {
    // `baseAddress` is `nil`
}
2020-07-07