Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 263
Edward Avatar asked Apr 23 '15 05:04

Edward


1 Answers

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`
}
like image 79
Martin R Avatar answered Nov 07 '22 03:11

Martin R