无法让CISourceOverCompositing工作

时间:2015-05-15 06:33:18

标签: ios avfoundation cifilter

我使用CISourceOverCompositing合并图像并导出为CVPixelBuffer。当图像数量低于41时,一切正常。但是当它达到42或更高时,结果变成全黑色。继承我的处理代码:

- (CVPixelBufferRef)pixelBufferRefWithMergingStillMaterials:(NSArray *)materials withBackgroundSampleBuffer:(CMSampleBufferRef)sampleBuffer {
// 基于gpu合并所有图层

CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

size_t width = CVPixelBufferGetWidth(pixelBuffer);
size_t height = CVPixelBufferGetHeight(pixelBuffer);

CIImage *backgroundImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];

static int i = 0;
i=0;
CIFilter *filter = [CIFilter filterWithName:@"CISourceOverCompositing"];
// 合并静态素材到一个CIImage
for (id<MicroVideoStillMaterial> stillMaterial in materials) {

    CIImage *foregroundImage = [stillMaterial image];


    [filter setDefaults];
    [filter setValue:backgroundImage forKey:kCIInputBackgroundImageKey];
    [filter setValue:foregroundImage forKey:kCIInputImageKey];

    backgroundImage = filter.outputImage;
    i++;
}

NSLog(@"iiiiiiiiiii  %d", i);

EAGLContext *glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
CIContext *context = [CIContext contextWithEAGLContext:glContext];

CFDictionaryRef empty = CFDictionaryCreate(kCFAllocatorDefault, // EMPTY IOSURFACE DICT
                                           NULL,
                                           NULL,
                                           0,
                                           &kCFTypeDictionaryKeyCallBacks,
                                           &kCFTypeDictionaryValueCallBacks);
CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
                                                              1,
                                                              &kCFTypeDictionaryKeyCallBacks,
                                                              &kCFTypeDictionaryValueCallBacks);

CFDictionarySetValue(attributes,kCVPixelBufferIOSurfacePropertiesKey,empty);

CVPixelBufferRef outputPixelBuffer = NULL;

CVPixelBufferCreate(kCFAllocatorSystemDefault, width, height, kCVPixelFormatType_32BGRA, attributes, &outputPixelBuffer);
CVPixelBufferLockBaseAddress(outputPixelBuffer, 0);
[context render:backgroundImage toCVPixelBuffer:outputPixelBuffer];

CVPixelBufferUnlockBaseAddress(outputPixelBuffer, 0);

return outputPixelBuffer;
}

并且这段代码在后台线程中运行。任何人都有任何想法请帮助。谢谢

0 个答案:

没有答案