5

我正在尝试将图像裁剪CMSampleBufferRef为特定大小。我正在做 5 个步骤 - 1.PixelBufferSampleBuffer2. 转换PixelBufferCIImage3. 裁剪CIImage4. 渲染CIImagePixelBuffer5. 附加PixelBufferSampleBuffer. 到目前为止,我在第 4 步遇到问题 - 将图像渲染回PixelBuffer(无法检查超出这一点)没有渲染到缓冲区(我使用相同的方法检查它CIImage imageWithCVPixelBuffer并获得 NULL 作为返回)。非常感谢任何提示或帮助。

CGRect cropRect = CGRectMake(0, 0, 640, 480);

CIImage *ciImage = [CIImage imageWithCVPixelBuffer:(CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer)]; //options: [NSDictionary dictionaryWithObjectsAndKeys:[NSNull null], kCIImageColorSpace, nil]];
ciImage = [ciImage imageByCroppingToRect:cropRect];    

CVPixelBufferRef pixelBuffer;
CVPixelBufferCreate(kCFAllocatorSystemDefault, 640, 480, kCVPixelFormatType_32BGRA, NULL, &pixelBuffer);

CVPixelBufferLockBaseAddress( pixelBuffer, 0 );

CIContext * ciContext = [CIContext contextWithOptions: nil];
[ciContext render:ciImage toCVPixelBuffer:pixelBuffer];
CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );

CMSampleTimingInfo sampleTime = {
    .duration = CMSampleBufferGetDuration(sampleBuffer),
    .presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer),
    .decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer)
};

CMVideoFormatDescriptionRef videoInfo = NULL;
CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, &videoInfo);

CMSampleBufferRef oBuf;
CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, true, NULL, NULL, videoInfo, &sampleTime, &oBuf);
4

1 回答 1

5

所以我找到了我遇到问题的原因。显然,我必须在初始化像素缓冲区时添加 kCVPixelBufferIOSurfacePropertiesKey 键,方法是创建字典并将其传递给初始化程序。如果有人偶然发现此问题,这是一个代码:

CFDictionaryRef empty; // empty value for attr value.
CFMutableDictionaryRef attrs;
empty = CFDictionaryCreate(kCFAllocatorDefault, // our empty IOSurface properties dictionary
                           NULL,
                           NULL,
                           0,
                           &kCFTypeDictionaryKeyCallBacks,
                           &kCFTypeDictionaryValueCallBacks);
attrs = CFDictionaryCreateMutable(kCFAllocatorDefault,
                                  1,
                                  &kCFTypeDictionaryKeyCallBacks,
                                  &kCFTypeDictionaryValueCallBacks);

CFDictionarySetValue(attrs,
                     kCVPixelBufferIOSurfacePropertiesKey,
                     empty);

然后您只需将此字典作为参数传递给 CVPixelBufferCreate

我在这里找到了这个解决方案:http: //allmybrain.com/2011/12/08/rendering-to-a-texture-with-ios-5-texture-cache-api/

于 2013-03-29T22:35:32.883 回答