Edward
Edward

Reputation: 241

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?

Objective-C

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

Swift attempt

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

Upvotes: 5

Views: 3570

Answers (1)

Martin R
Martin R

Reputation: 540055

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), Checked for Swift 5 (Xcode 11):

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

Upvotes: 15

Related Questions