好吧,我想我终于明白了。我创建了一个辅助扩展程序来制作的完整副本
CVPixelBuffer:
extension CVPixelBuffer { func copy() -> CVPixelBuffer { precondition(CFGetTypeID(self) == CVPixelBufferGetTypeID(), "copy() cannot be called on a non-CVPixelBuffer") var _copy : CVPixelBuffer? CVPixelBufferCreate( nil, CVPixelBufferGetWidth(self), CVPixelBufferGetHeight(self), CVPixelBufferGetPixelFormatType(self), CVBufferGetAttachments(self, kCVAttachmentMode_ShouldPropagate)?.takeUnretainedValue(), &_copy) guard let copy = _copy else { fatalError() } CVPixelBufferLockbaseAddress(self, kCVPixelBufferLock_ReadOnly) CVPixelBufferLockbaseAddress(copy, 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, 0) CVPixelBufferUnlockbaseAddress(self, kCVPixelBufferLock_ReadOnly) return copy }}现在您可以在您的
didOutputSampleBuffer方法中使用它:
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }let copy = pixelBuffer.copy()toProcess.append(copy)但是请注意,一个这样的pixelBuffer会占用大约3MB的内存(1080p),这意味着在100帧中您已经拥有了300MB的内存,这大约是iPhone所说的STAHP(并崩溃)的时间。
请注意,您实际上并不想复制,
CMSampleBuffer因为它实际上只包含一个,
CVPixelBuffer因为它是一个图像。



