赞
踩
首先,Open Graphics Library(OpenGL)用于二维和三维的可视化。而OpenGL for Embedded Systems(OpenGL ES)是OpenGL的一个简化版本,消除冗余的功能并提供一个库,非常容易学习,在移动装置上应用实践容易。
它直接运行的图形处理硬件上,也就是GPU,并不运行在CPU上,因此,对CPU的消耗非常的小,并不影响程序的运行速度。如果是罗列出各种滤镜,反而吃cpu,让程序运行速度慢。
其次,OpenGLES是基于C语言编写的,与平台无关的应用程序接口。
因此,进行大量渲染的时候,通常使用OpenGLES
下面是一个例子:
#import "ViewController.h"
#import <GLKit/GLKit.h>
@interface ViewController ()
@property (strong, nonatomic) GLKView *glkView;//渲染用的buffer视图
@property (strong, nonatomic) CIFilter *filter;
@property (strong, nonatomic) CIImage *ciImage;
@property (strong, nonatomic) CIContext *ciContext;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *showImage = [UIImage imageNamed:@"IMG_0160"];
CGRect rect = CGRectMake(0, 0, 350, 400);
// 获取OpenGLES上下文
EAGLContext *eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
//创建出渲染的buffer
_glkView = [[GLKView alloc] initWithFrame:rect context:eaglContext];
[_glkView bindDrawable];//绑定绘制
[self.view addSubview:_glkView];
//创建出CoreImage用的上下文
_ciContext = [CIContext contextWithEAGLContext:eaglContext options:@{kCIContextWorkingColorSpace:[NSNull null]}];
//CoreImage相关设置
_ciImage = [[CIImage alloc] initWithImage:showImage];
_filter = [CIFilter filterWithName:@"CISepiaTone"];// //棕黑色调
[_filter setValue:_ciImage forKey:kCIInputImageKey];
[_filter setValue:@(0) forKey:kCIInputIntensityKey];
//开始渲染
[_ciContext drawImage:[_filter outputImage] inRect:CGRectMake(0, 0, _glkView.drawableWidth, _glkView.drawableHeight) fromRect:[_ciImage extent]];
[_glkView display];
// [self filter];
//动态渲染
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 450, 320, 20)];
slider.minimumValue = 0.f;
slider.maximumValue = 1.f;
[slider addTarget:self action:@selector(sliderEvent:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
}
- (void)sliderEvent:(UISlider *)slider
{
_filter = [CIFilter filterWithName:@"CISepiaTone"];// //棕黑色调
[_filter setValue:_ciImage forKey:kCIInputImageKey];
[_filter setValue:@(slider.value) forKey:kCIInputIntensityKey];
//开始渲染
[_ciContext drawImage:[_filter outputImage] inRect:CGRectMake(0, 0, _glkView.drawableWidth, _glkView.drawableHeight) fromRect:[_ciImage extent]];
[_glkView display];
}
@end
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。