Edward Edward - 1 year ago 249
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)

Answer Source

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`
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download