IOS CoreImage dynamic rendering (filter)

Source: Internet
Author: User

IOS CoreImage dynamic rendering (filter)

//

// ViewController. m

// CoreImageOfDong

//

// Created by Dong on 15/6/30.

// Copyright (c) 2015 xindong. All rights reserved.

//

# Import ViewController. h

# Import // Import this database

@ Interface ViewController ()

@ Property (nonatomic, strong) GLKView * glkView; // buffer view for rendering (similar to streaming media, real-time change)

@ 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 );

 

// Obtain the context of OpenGLES Rendering

EAGLContext * eagContext = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];

 

// Create the rendered buffer

Self. glkView = [[GLKView alloc] initWithFrame: rect context: eagContext];

[Self. glkView bindDrawable]; // bind the drawing; otherwise, the screen is black at the beginning.

[Self. view addSubview: self. glkView];

 

// Create the context for CoreImage

Self. ciContext = [CIContext contextWithEAGLContext: eagContext options :@{ kCIContextWorkingColorSpace: [NSNull null]}];

 

// CoreImage settings

Self. ciImage = [[CIImage alloc] initWithImage: showImage];

Self. filter = [CIFilter filterWithName: @ CISepiaTone];

[Self. filter setValue: self. ciImage forKey: kCIInputImageKey];

[Self. filter setValue: @ (0) forKey: kCIInputIntensityKey];

 

// Start Rendering

[Self. ciContext drawImage: [self. filter valueForKey: kCIOutputImageKey] inRect: CGRectMake (0, 0, self. glkView. drawableWidth, self. glkView. drawableHeight) fromRect: [self. ciImage extent];

[Self. glkView display];

 

// Dynamic Rendering

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

// Start Rendering

[Self. ciContext drawImage: [self. filter valueForKey: kCIOutputImageKey] inRect: CGRectMake (0, 0, self. glkView. drawableWidth, self. glkView. drawableHeight) fromRect: [self. ciImage extent];

[Self. glkView display];

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.