Edward Edward - 1 month ago 20x
Swift Question

How to convert CVPixelBufferGetBaseAddress call to Swift?

Maybe I'm the first person doing this in Swift but there seems to be nothing on the net using &/inout together with uint8_t in Swift. Could someone translate this please? Is this relationship bitwise?


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

Swift attempt

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


CVPixelBufferGetBaseAddress() returns a UnsafeMutablePointer<Void>, which can be converted to an UInt8 pointer via

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

Update for Swift 3 (Xcode 8):

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