如何将CVPixelBufferGetBaseAddress调用转换为Swift?

时间:2015-04-23 05:06:07

标签: objective-c swift

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

目标-C

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

快速尝试

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

1 个答案:

答案 0 :(得分:10)

CVPixelBufferGetBaseAddress()返回UnsafeMutablePointer<Void>, 可以通过

转换为UInt8指针
let buf = UnsafeMutablePointer<UInt8>(CVPixelBufferGetBaseAddress(pixelBuffer))

更新Swift 3(Xcode 8):

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