标签:
首先OpenGL2框架的使用需要
#import <GLKit/GLKit.h>
OpenGL 调用的是GPU,而不是CPU所以,在不断改变属性的时候不会出现卡顿。
大概的调用方法分为 :
// 1.首先GLK 需要一个渲染环境 , 先创建一个EAGLContext渲染环境, GLKView 初始化的时候需要使用
// 2.初始化 GLKView 对象,将GLKView绑定并且添加到View里
// 3.在GLKView中渲染是通过 CIContext CIContext drawImage inRect fromRect
// 4.最后通过GLKView展现出来 [mglkView display];
然后代码: 这是写在Model里的代码,因为在Model里无法直接获取View,所以需要通过 UIApplication 获取到当前的View
-(void)openGLCreateGLKViewWithImage:(UIImage *)image FilterName:(NSString *)filterName Rect:(CGRect)rect
{
// 获取当前的View
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
if(window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow *tmpWin in windows)
{
if(tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
NSArray *viewsArray = [window subviews];
UIView *frontView;
if ([viewsArray count]>0) {
frontView = [viewsArray objectAtIndex:0];
}
mrect = rect;
EAGLContext *ueaglContext = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2]; // 首先是渲染环境
mglkView = [[GLKView alloc]initWithFrame:rect context:ueaglContext];
[mglkView bindDrawable]; // 绑定 mglkView
if (frontView) {
[frontView addSubview:mglkView]; // 将GLKView的对象添加到当前View里面
}
mcontext = [CIContext contextWithEAGLContext:ueaglContext options:@{kCIContextWorkingColorSpace :[NSNull null]}];
mimage = [[CIImage alloc]initWithImage:image];
mfilter = [CIFilter filterWithName:filterName];
}
上面是初始化对象的代码, 然后下面的方法是给对象设置属性
-(void)openGLdrawImageWithValue:(CGFloat)value key:(NSString *)keyName
{
[mfilter setValue:@(value) forKey:keyName]; //keyName 可以通过 NSlog(mfilter.attributes) 查询 设置
[mfilter setValue:mimage forKey:kCIInputImageKey]; // 设置滤镜的输入CIImage CIFilter的图片输入格式是 CIImage
[mcontext drawImage:[mfilter valueForKey:kCIOutputImageKey]
inRect:CGRectMake(0, 0, mglkView.drawableWidth, mglkView.drawableHeight)
fromRect:[mimage extent]]; // 通过 CIContext绘制,将图片展现在GLKView的对象中
[mglkView display]; // 最后 display
}
标签:
原文地址:http://www.cnblogs.com/stuwan/p/4986307.html