Hold video frames of a AVCaptureVideoDataOutputSampleBufferDelegate as array.

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
//If you keep it in the array (buffers) like this, you can not get a new frame


You can free the memory pool by converting to CVPixelBuffer and making a deep copy.

extension CVPixelBuffer {
func copy() -> CVPixelBuffer {
precondition(CFGetTypeID(self) == CVPixelBufferGetTypeID(), "copy() cannot be called on a non-CVPixelBuffer")

var _copy : CVPixelBuffer?
CVBufferGetAttachments(self, CVAttachmentMode(rawValue: 1)!),

guard let copy = _copy else { fatalError() }

CVPixelBufferLockBaseAddress(self, CVPixelBufferLockFlags(rawValue: 0x00000001))
CVPixelBufferLockBaseAddress(copy, CVPixelBufferLockFlags(rawValue: 0))

for plane in 0..<CVPixelBufferGetPlaneCount(self) {
let dest = CVPixelBufferGetBaseAddressOfPlane(copy, plane)
let source = CVPixelBufferGetBaseAddressOfPlane(self, plane)
let height = CVPixelBufferGetHeightOfPlane(self, plane)
let bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(self, plane)

memcpy(dest, source, height * bytesPerRow)

CVPixelBufferUnlockBaseAddress(copy, CVPixelBufferLockFlags(rawValue: 0))
CVPixelBufferUnlockBaseAddress(self, CVPixelBufferLockFlags(rawValue: 0))

return copy
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
let copiedBuffer = pixelBuffer.copy()
// Hold as an array of CVPixelBuffer.
  • We send information related to machine learning.
  • Twitter
  • contact:
  • rockyshikoku@gmail.com



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store