濾鏡:可以用來給圖像添加效果。功能很強大济榨,使用濾鏡很容易對圖像坯沪、視頻幀進行模糊、色彩擒滑、像素等處理
介紹
1腐晾、框架:
1)使用CoreImage框架【現(xiàn)在已經不需要手動導入,因為系統(tǒng)已經完成】
2)是一個圖像框架丐一,它基于OpenGL頂層創(chuàng)建藻糖,底層則用著色器來處理圖像
3)它利用GPU基于硬件加速來處理圖像
4)CoreImage中有很多濾鏡
5)它們能夠一次給予一張圖片或視頻幀多種視覺效果【叫做濾鏡鏈】
6)濾鏡可以連接起來組成一個濾鏡鏈,把濾鏡效果疊加起來處理圖像
濾鏡鏈:濾鏡的嵌套組合使用
2库车、類:
1)CIImage:用于保存圖像數據的類巨柒;
CGImageRef:圖像中的數據
2)CIFilter:濾鏡類,根據圖片屬性進行細節(jié)處理的類柠衍。它對所有像素進行操作洋满,用鍵-值(KVC)來設置
3)CIContext:上下文,是實現(xiàn)對圖像處理的具體對象 ——>濾鏡對象輸出的圖像并不是和成之后的圖像珍坊,需要使用圖像處理的上下文合并輸出的圖像
3牺勾、效果:
【100+效果可以通過CIFilter的屬性attributes查找需要設置的參數內容】
1)按效果分類:
kCICategoryDistortionEffect 扭曲效果,比如bump阵漏、旋轉驻民、hole
kCICategoryGeometryAdjustment 幾何開著調整,比如仿射變換袱饭、平切川无、透視轉換
kCICategoryCompositeOperation 合并,比如源覆蓋(source over)虑乖、最小化、源在頂(source atop)晾虑、色彩混合模式
kCICategoryHalftoneEffect Halftone效果疹味,比如screen、line screen帜篇、hatched
kCICategoryColorAdjustment 色彩調整糙捺,比如伽馬調整、白點調整笙隙、曝光
kCICategoryColorEffect 色彩效果洪灯,比如色調調整、posterize
kCICategoryTransition 圖像間轉換竟痰,比如dissolve签钩、disintegrate with mask掏呼、swipe
kCICategoryTileEffect 瓦片效果,比如parallelogram铅檩、triangle
kCICategoryGenerator 圖像生成器憎夷,比如stripes、constant color昧旨、checkerboard
kCICategoryGradient 漸變拾给,比如軸向漸變、仿射漸變兔沃、高斯?jié)u變
kCICategoryStylize 風格化蒋得,比如像素化、水晶化
kCICategorySharpen 銳化乒疏、發(fā)光
kCICategoryBlur 模糊窄锅,比如高斯模糊、焦點模糊缰雇、運動模糊
2)按使用場景分類:
kCICategoryStillImage 用于靜態(tài)圖像
kCICategoryVideo 用于視頻
kCICategoryInterlaced 用于交錯圖像
kCICategoryNonSquarePixels 用于非矩形像素
kCICategoryHighDynamicRange 用于HDR
簡單的使用
1入偷、查詢效果分類里面的效果[CIFilter filterNamesInCategory:@""];
(1)按住command鍵 點擊CIFilter 進入接口文件 找到第128行-148行全部都是 效果分類
(2)選擇其中某一個分類 NSLog -> [CIFilter filterNamesInCategory:剛才拷貝的分類]; -> 打印出來的 是這個分類包含的所有 效果 -> 拷貝選擇其中的某一個效果
2、查詢效果的屬性[CIFilter filterWithName:XXX].attributes
NSLog -> [CIFilter filterWithName:剛才拷貝選擇其中的某一個效果].attributes ->得到這個濾鏡所有可以設置的屬性
3械哟、使用步驟
1)初始化CIImage:
2)創(chuàng)建CIFilter濾鏡并給濾鏡設置屬性(KVC)
3)創(chuàng)建CIContext上下文
4)合并濾鏡輸出的圖像——>得到一個合并之后的圖像
5)賦值給UIImageView對象驚醒顯示
6)如果要使用濾鏡鏈疏之,可以再次疊加效果
代碼實例
#import "ViewController.h"
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
UIImageView *imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
//添加一個進入相冊的按鈕
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 40);
[button setTitle:@"進入相冊" forState:UIControlStateNormal];
button.backgroundColor = [UIColor brownColor];
[button addTarget:self action:@selector(doit) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//添加濾鏡按鈕
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(100, 150, 100, 40);
[button1 setTitle:@"Filter" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor brownColor];
[button1 addTarget:self action:@selector(addColorFilter) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
使用濾鏡進行顏色處理
-(void)addColorFilter{
CIImage *inputImage = [CIImage imageWithCGImage:imageView.image.CGImage];
CIFilter *filter = [CIFilter filterWithName:@"CIColorMonochrome"];
[filter setValue:inputImage forKey:kCIInputImageKey];
NSLog(@"顏色%@",[CIFilter filterNamesInCategory:kCICategoryColorEffect]);
NSLog(@"%@",filter.attributes);
[filter setValue:[CIColor colorWithRed:1.0 green:0.7262 blue:0.1014 alpha:1] forKey:kCIInputColorKey];
[filter setValue:@0.5 forKey:kCIInputIntensityKey];
// CIContext *context = [CIContext contextWithOptions:nil];
CIImage *outputImage = filter.outputImage;
// CGImageRef imageRef =[context createCGImage:outputImage fromRect:outputImage.extent];
// imageView.image = [UIImage imageWithCGImage:imageRef];
[self addFilterLinkerWithImage:outputImage];
}
//再次添加濾鏡->濾鏡鏈
-(void)addFilterLinkerWithImage:(CIImage*)image{
CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"];
[filter setValue:image forKey:kCIInputImageKey];
[filter setValue:@0.5 forKey:kCIInputIntensityKey];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef imageRef = [context createCGImage:filter.outputImage fromRect:filter.outputImage.extent];
imageView.image = [UIImage imageWithCGImage:imageRef];
// 把添加好濾鏡效果的圖片 保存到相冊【注意:不可以直接保存outputImage->因為這是一個沒有吧濾鏡效果和原圖合成的圖像】
UIImageWriteToSavedPhotosAlbum(imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
添加濾鏡效果核心代碼
-(void)addFilter{
//1.輸入的源圖->先得到CGImage,在得到CIImage
CIImage *inputImage = [CIImage imageWithCGImage:imageView.image.CGImage];
// 2.創(chuàng)建濾鏡 并設置濾鏡的效果
CIFilter *filter = [CIFilter filterWithName:@"CIBumpDistortion"];
// 給濾鏡添加inputImage
[filter setValue:inputImage forKey:kCIInputImageKey];
//設置凹凸效果的半徑暇咆,半徑越大效果越明顯
[filter setValue:@500 forKey:kCIInputRadiusKey];
// CIVector:表示x锋爪、y的坐標
[filter setValue:[CIVector vectorWithX:200 Y:200] forKey:kCIInputCenterKey];//設置中心點XY
// 3.合并 原圖和濾鏡的效果
CIImage *outPutImage = filter.outputImage;
// 創(chuàng)建圖像操作的上下文
CIContext *context = [CIContext contextWithOptions:nil];
// 合成一個包含原圖和濾鏡效果的圖像
// outPutImage.extent可以獲得圖像的尺寸
// 1.image -> 濾鏡輸出的圖像
// 2.fromRect -> 合成之后圖像的尺寸 :圖像.extent
CGImageRef imageRef = [context createCGImage:outPutImage fromRect:outPutImage.extent];
imageView.image = [UIImage imageWithCGImage:imageRef];
}
進入相冊獲取源圖的方法
-(void)doit{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
// picker.sourceType = 可以設置是攝像還是相冊
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//獲得info中的image 并顯示在imageView上
UIImage *image = info[UIImagePickerControllerOriginalImage];
imageView.image = image;
// 讓相冊消失
[self dismissViewControllerAnimated:YES completion:nil];