我正在尝试将CMSampleBufferRef
中的图片裁剪为特定尺寸。我正在制作5个步骤 - 1.从PixelBuffer
获取SampleBuffer
2.将PixelBuffer
转换为CIImage
3.裁剪CIImage
4.渲染CIImage
返回PixelBuffer
5.将PixelBuffer
附加到SampleBuffer
。到目前为止,我遇到了第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);
答案 0 :(得分:4)
所以我找到了我遇到问题的原因。显然我必须在初始化像素缓冲区时添加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/