带有核心图像过滤器的EAGLContext

时间:2014-01-13 04:38:00

标签: ios core-image caeagllayer eaglcontext

我想实时向摄像机视频输入添加图像,但只需显示它们,无需保存。如果我使用标准的核心图像程序它工作正常,但我需要更多的帧速率。但是,如果我取消注释这个代码并接下来发表评论,那就没有了。 OpenGLView只是一个带有方法

UIView子类
+(Class)layerClass {
return [CAEAGLLayer class];
}

//Working code
EAGLContext *myEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
NSDictionary *options = @{ kCIContextWorkingColorSpace : [NSNull null] };
CIContext *context = [CIContext contextWithEAGLContext:myEAGLContext options:options];
UIImage* foto = [UIImage imageNamed:@"foto2.jpg"];
UIImage* foto2 = [UIImage imageNamed:@"foto_no_ok.png"];
CIImage *backgroundImage = [[CIImage alloc]initWithImage:foto];
CIImage *foregroundImage = [[CIImage alloc]initWithImage:foto2];
 CIFilter *myFilter = [CIFilter filterWithName:@"CISourceOverCompositing" keysAndValues: kCIInputImageKey, foregroundImage, kCIInputBackgroundImageKey, backgroundImage, nil];
CIImage* resultingImage = [myFilter outputImage];

/* Not working code, GPU processing
self.view = [[OpenGLView alloc]initWithFrame:self.view.frame];
[self.view setOpaque:YES];
[self.view setFrame:CGRectMake(0, 0, 500, 500)];
[context drawImage:resultingImage inRect:self.view.bounds fromRect:self.view.bounds];
 */

//Working code, CPU processing
CGRect extent = [resultingImage extent];
CGImageRef cgImage = [context createCGImage:resultingImage fromRect:extent];
UIImage* myImage = [[UIImage alloc]initWithCGImage:cgImage];
self.myImageView.image = myImage;
CFRelease(cgImage);

缺少什么?我做错了什么?非常感谢你。

1 个答案:

答案 0 :(得分:4)

好的,最后我明白了。切记不要将此代码放在viewDidLoad:中。图像需要一些工作,但这是一个不同的主题。

EAGLContext *myEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
NSDictionary *options = @{ kCIContextWorkingColorSpace : [NSNull null] };
self.context = [CIContext contextWithEAGLContext:myEAGLContext options:options];
[EAGLContext setCurrentContext:myEAGLContext];
self.visorOpenGLView.context = myEAGLContext;

UIImage* foto = [UIImage imageNamed:@"onepic.jpg"];
UIImage* foto2 = [UIImage imageNamed:@"anotherpic.png"];
CIImage *backgroundImage = [[CIImage alloc]initWithImage:foto];
CIImage *foregroundImage = [[CIImage alloc]initWithImage:foto2];
CIFilter *myFilter = [CIFilter filterWithName:@"CISourceOverCompositing" keysAndValues: kCIInputImageKey, foregroundImage, kCIInputBackgroundImageKey, backgroundImage, nil];
self.resultingImage = [myFilter outputImage];
UIImage* result = [UIImage imageWithCIImage:self.resultingImage];
self.resultingImageView.image = result;

CGRect ext = [foregroundImage extent];
[self.visorOpenGLView bindDrawable];
[self.context drawImage:self.resultingImage inRect:self.visorOpenGLView.frame fromRect:ext];
[self.visorOpenGLView display];