EAGLContext与核心图像滤镜

3

我希望能够实时向相机视频流中添加图像,但只需要显示它们,不需要保存。如果我使用标准的Core Image过程,它可以正常工作,但是我需要更高的帧率。但是,如果我取消注释此代码并注释下一个代码,则无法正常工作。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个回答

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];

注意:OpenGL 正在被弃用,因此请改用 Metal。 - Tom Roggero

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接