我正在iOS应用中录制实时视频.在另一个StackOverflow页面(
Link)上,我发现你可以使用vImage_Buffer来处理我的帧.
问题是我不知道如何从输出的vImage_buffer返回CVPixelBufferRef.
以下是另一篇文章中给出的代码:
NSInteger cropX0 = 100,cropY0 = 100,cropHeight = 100,cropWidth = 100,outWidth = 480,outHeight = 480; CVImageBufferRef imageBuffer = CMSampleBufferGetimageBuffer(sampleBuffer); CVPixelBufferLockBaseAddress(imageBuffer,0); void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); vImage_Buffer inBuff; inBuff.height = cropHeight; inBuff.width = cropWidth; inBuff.rowBytes = bytesPerRow; int startpos = cropY0*bytesPerRow+4*cropX0; inBuff.data = baseAddress+startpos; unsigned char *outImg= (unsigned char*)malloc(4*outWidth*outHeight); vImage_Buffer outBuff = {outImg,outHeight,outWidth,4*outWidth}; vImage_Error err = vImageScale_ARGB8888(&inBuff,&outBuff,NULL,0); if (err != kvImageNoError) NSLog(@" error %ld",err);
现在我需要将outBuff转换为CVPixelBufferRef.
我假设我需要使用vImageBuffer_copyToCVPixelBuffer,但我不确定如何.
我的第一次尝试失败了EXC_BAD_ACCESS:
CVPixelBufferUnlockBaseAddress(ImageBuffer的,0);
CVPixelBufferRef pixelBuffer; CVPixelBufferCreate(kcfAllocatorSystemDefault,480,kCVPixelFormatType_32BGRA,&pixelBuffer); CVPixelBufferLockBaseAddress( pixelBuffer,0 ); vImage_CGImageFormat format = { .bitsPerComponent = 8,.bitsPerPixel = 32,.bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaNoneskipFirst,//BGRX8888 .colorSpace = NULL,//sRGB }; vImageBuffer_copyToCVPixelBuffer(&outBuff,&format,pixelBuffer,kvImageNoFlags); // Here is the crash! CVPixelBufferUnlockBaseAddress( pixelBuffer,0 );
任何的想法?
解决方法
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool : YES],kCVPixelBufferCGImageCompatibilityKey,[NSNumber numberWithBool : YES],kCVPixelBufferCGBitmapContextCompatibilityKey,[NSNumber numberWithInt : 480],kCVPixelBufferWidthKey,kCVPixelBufferHeightKey,nil]; status = CVPixelBufferCreateWithBytes(kcfAllocatorDefault,outImg,bytesPerRow,(__bridge CFDictionaryRef)options,&pixbuffer);
你应该生成一个像上面这样的新pixelBuffer.