我正在尝试在CMSampleBufferRef中裁剪图像到特定的大小。我正在进行5个步骤- 1.从SampleBuffer获取PixelBuffer 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);发布于 2013-03-30 06:35:33
所以我找到了我有问题的原因。显然,在初始化像素缓冲区时,我必须添加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/
https://stackoverflow.com/questions/15693784
复制相似问题