首要要確定CVPixelbuffer的類(lèi)型, 有 YUV/RGBA 的等等
從攝像頭取BGRA式例代碼:
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
//BGRA格式的cvpixelbuffer中獲取bgra
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
size_t bufferSize = bytesPerRow * height;
uint8_t *myPixelBuf = (uint8_t *)malloc(bufferSize);
memmove(myPixelBuf, baseAddress, bufferSize);
baseAddress = nil;
IFrameUploaded cb = callback;
[imageEngine pushNewFrame:myPixelBuf dataType:IMAGE_BGRA8888 withWidth:(int)width withHeight:(int)height withSize:(int)bufferSize frameUploadedNotify:cb withTimestamp:0 ctx:myPixelBuf];
}