Skip to content

Instantly share code, notes, and snippets.

@valkjsaaa
Created May 19, 2016 09:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valkjsaaa/f9edfc25b4fd592caf82834fafc07759 to your computer and use it in GitHub Desktop.
Save valkjsaaa/f9edfc25b4fd592caf82834fafc07759 to your computer and use it in GitHub Desktop.
deep copy of a CVImageBuffer
extension CVPixelBuffer {
func deepcopy() -> CVPixelBuffer? {
let width = CVPixelBufferGetWidth(self)
let height = CVPixelBufferGetHeight(self)
let format = CVPixelBufferGetPixelFormatType(self)
var pixelBufferCopyOptional:CVPixelBuffer?
CVPixelBufferCreate(nil, width, height, format, nil, &pixelBufferCopyOptional)
if let pixelBufferCopy = pixelBufferCopyOptional {
CVPixelBufferLockBaseAddress(self, kCVPixelBufferLock_ReadOnly)
CVPixelBufferLockBaseAddress(pixelBufferCopy, 0)
let baseAddress = CVPixelBufferGetBaseAddress(self)
let dataSize = CVPixelBufferGetDataSize(self)
print("dataSize: \(dataSize)")
let target = CVPixelBufferGetBaseAddress(pixelBufferCopy)
memcpy(target, baseAddress, dataSize)
CVPixelBufferUnlockBaseAddress(pixelBufferCopy, 0)
CVPixelBufferUnlockBaseAddress(self, kCVPixelBufferLock_ReadOnly)
}
return pixelBufferCopyOptional
}
}
@max-christian
Copy link

This crashes for me on an iPad Pro but works fine on an iPhone X.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment