1.CoreImage 濾鏡的使用(馬賽克模糊)ide
CoreImage是蘋果公司爲了簡化圖片處理的難度而開發出來的類庫。性能
隨着iOS版本號升級以及硬件性能的不斷提高,CoreImage將支持愈來愈多的濾鏡atom
- (void)hFilter {spa
// 0. 導入CIImage圖片3d
CIImage *ciImage = [[CIImagealloc] initWithImage:[UIImageimageNamed:@"demo"]];orm
// 1. 建立出Filter濾鏡圖片
CIFilter *filter = [CIFilterfilterWithName:@"CIPixellate"];ci
[filtersetValue:ciImage開發
forKey:kCIInputImageKey];圖片處理
NSLog(@"%@", filter.attributes);
[filtersetDefaults];
CIImage *outImage = [filter valueForKey:kCIOutputImageKey];
// 2. 用CIContext將濾鏡中的圖片渲染出來
CIContext *context = [CIContextcontextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:outImage
fromRect:[outImage extent]];
// 3. 導出圖片
UIImage *showImage = [UIImageimageWithCGImage:cgImage];
CGImageRelease(cgImage);
// 4. 載入出來
UIImageView *imageView = [[UIImageViewalloc] initWithImage:showImage];
imageView.center =self.view.center;
[self.viewaddSubview:imageView];
}
2.CoreImage 濾鏡的組合
不一樣的濾鏡可以組合在一塊兒使用。
可以動態的改動濾鏡組合中單個濾鏡的參數來實現一種動態調整的效果
- (void)complexCoreImage {
// 0. 導入CIImage圖片
CIImage *ciImage = [[CIImagealloc] initWithImage:[UIImageimageNamed:@"demo"]];
// 1. 建立出Filter濾鏡
CIFilter *filterOne = [CIFilterfilterWithName:@"CIPixellate"];
[filterOnesetValue:ciImage
forKey:kCIInputImageKey];
[filterOnesetDefaults];
CIImage *outImage = [filterOne valueForKey:kCIOutputImageKey];
CIFilter *filterTwo = [CIFilterfilterWithName:@"CIHueAdjust"];
[filterTwosetValue:outImage
forKey:kCIInputImageKey];
[filterTwosetDefaults];
//默以爲0,不設置不能改變。可以封裝一個調用
[filterTwosetValue:@(3.14)
forKey:kCIInputAngleKey];
NSLog(@"%@",filterTwo.attributes);//可以看到一些參數 key
CIImage *outputImage = [filterTwo valueForKey:kCIOutputImageKey];
// 2. 用CIContext將濾鏡中的圖片渲染出來
CIContext *context = [CIContextcontextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:outputImage
fromRect:[outImage extent]];
// 3. 導出圖片
UIImage *showImage = [UIImageimageWithCGImage:cgImage];
CGImageRelease(cgImage);
// 4. 載入出來
UIImageView *imageView = [[UIImageViewalloc] initWithImage:showImage];
imageView.center =self.view.center;
[self.viewaddSubview:imageView];
}
3.在 OpenGLES 下進行渲染
在OpenGLES下進行濾鏡的渲染可以提升效率。(GPU渲染,不佔用CPU)
假設需要實時查看多個濾鏡動態渲染的效果,使用OpenGLES是一個好的選擇
#import <GLKit/GLKit.h>
@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 {
[superviewDidLoad];
UIImage *showImage = [UIImageimageNamed:@"demo"];
CGRect rect = CGRectMake(0, 0, showImage.size.width, showImage.size.height);
// 獲取OpenGLES渲染的上下文
EAGLContext *eagContext = [[EAGLContextalloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
// 建立出渲染的buffer
_glkView = [[GLKViewalloc] initWithFrame:rect
context:eagContext];
[_glkViewbindDrawable];
[self.viewaddSubview:_glkView];
// 建立出CoreImage用的上下文
_ciContext = [CIContextcontextWithEAGLContext:eagContext
options:@{kCIContextWorkingColorSpace : [NSNullnull]}];
// CoreImage相關設置
_ciImage = [[CIImagealloc] initWithImage:showImage];
_filter = [CIFilterfilterWithName:@"CISepiaTone"];
[_filtersetValue:_ciImageforKey:kCIInputImageKey];
[_filtersetValue:@(0)forKey:kCIInputIntensityKey];
// 開始渲染
[_ciContextdrawImage:[_filteroutputImage]
inRect:CGRectMake(0,0, _glkView.drawableWidth,_glkView.drawableHeight)
fromRect:[_ciImageextent]];
[_glkViewdisplay];
// 動態渲染
UISlider *slider = [[UISlideralloc] initWithFrame:CGRectMake(0,400, 320, 20)];
slider.minimumValue =0.f;
slider.maximumValue =1.f;
[slider addTarget:selfaction:@selector(sliderEvent:)forControlEvents:UIControlEventValueChanged];
[self.viewaddSubview:slider];
}
- (void)sliderEvent:(UISlider *)slider {
[_filtersetValue:_ciImageforKey:kCIInputImageKey];
[_filtersetValue:@(slider.value)
forKey:kCIInputIntensityKey];
// 開始渲染
[_ciContextdrawImage:[_filteroutputImage]
inRect:CGRectMake(0,0, _glkView.drawableWidth,_glkView.drawableHeight)
fromRect:[_ciImageextent]];
[_glkViewdisplay];
}