首页 > 解决方案 > 如何使用 CVPixelBufferCreate 创建黑色的 CMSampleBufferRef?

问题描述

我需要一个黑色CMSampleBufferRef来测试。我使用此代码。但它是白色的CMSampleBufferRef

CVPixelBufferRef pixelBuffer=NULL;
CVPixelBufferCreate(kCFAllocatorDefault, 1280, 720, kCVPixelFormatType_32BGRA, NULL, &pixelBuffer);
CMSampleTimingInfo info ={ kCMTimeInvalid, kCMTimeZero, kCMTimeInvalid };
CMFormatDescriptionRef formatDesc=NULL;
CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, &formatDesc);
CMSampleBufferRef sampleBuffer=NULL;
CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault,
                                         pixelBuffer,
                                         formatDesc,
                                         &info,
                                         &sampleBuffer);

如何使用 CVPixelBufferCreate 创建黑色的 CMSampleBufferRef?

标签: objective-cswift

解决方案


如果在代码之后添加它,它将用黑色像素填充像素缓冲区:

    CVPixelBufferLockBaseAddress(pixelBuffer, 0);
    size_t width = CVPixelBufferGetWidth(pixelBuffer);
    size_t height = CVPixelBufferGetHeight(pixelBuffer);
    UInt32* buffer = (UInt32*)CVPixelBufferGetBaseAddress(pixelBuffer);
    for ( unsigned long i = 0; i < width * height; i++ )
    {
        buffer[i] = CFSwapInt32HostToBig(0x000000ff);
    }
    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);

推荐阅读