//
// ViewController.m
// CoreImageOfDong
//
// Created by Dong on 15/6/30.
// Copyright (c) 2015年 xindong. All rights reserved.
//
#import ViewController.h
#import
@interface ViewController ()
@property (nonatomic, strong) GLKView *glkView; //渲染用的buffer視圖(類似流媒體,實時改變)
@property (nonatomic, strong) CIFilter *filter;
@property (nonatomic, strong) CIImage *ciImage;
@property (nonatomic, strong) CIContext *ciContext;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *showImage = [UIImage imageNamed:@demo1.jpg];
CGRect rect = CGRectMake(0, 0, showImage.size.width, showImage.size.height);
// 獲取OpenGLES渲染的上下文
EAGLContext *eagContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
// 創建出渲染的buffer
self.glkView = [[GLKView alloc] initWithFrame:rect context:eagContext];
[self.glkView bindDrawable]; // 綁定繪制,否則剛開始會黑屏
[self.view addSubview:self.glkView];
// 創建出CoreImage用的上下文
self.ciContext = [CIContext contextWithEAGLContext:eagContext options:@{kCIContextWorkingColorSpace : [NSNull null]}];
// CoreImage相關設置
self.ciImage = [[CIImage alloc] initWithImage:showImage];
self.filter = [CIFilter filterWithName:@CISepiaTone];
[self.filter setValue:self.ciImage forKey:kCIInputImageKey];
[self.filter setValue:@(0) forKey:kCIInputIntensityKey];
// 開始渲染
[self.ciContext drawImage:[self.filter valueForKey:kCIOutputImageKey] inRect:CGRectMake(0, 0, self.glkView.drawableWidth, self.glkView.drawableHeight) fromRect:[self.ciImage extent]];
[self.glkView display];
// 動態渲染
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(30, 500, 200, 10)];
slider.minimumValue = 0.f;
slider.maximumValue = 10.f;
[slider addTarget:self action:@selector(changeColorValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
}
- (void)changeColorValue:(UISlider *)slider
{
[self.filter setValue:self.ciImage forKey:kCIInputImageKey];
[self.filter setValue:@(slider.value) forKey:kCIInputIntensityKey];
// 開始渲染
[self.ciContext drawImage:[self.filter valueForKey:kCIOutputImageKey] inRect:CGRectMake(0, 0, self.glkView.drawableWidth, self.glkView.drawableHeight) fromRect:[self.ciImage extent]];
[self.glkView display];
}