转载自:http://blog.sina.com.cn/s/blog_5fb39f9101018gv7.html
直接上代码了:
//
// ViewController.m
// 图片模糊处理
//
// Created by wusiping on 16/1/28.
// Copyright © 2016年 wusiping. All rights reserved.
// #import "ViewController.h"
#import <AssetsLibrary/AssetsLibrary.h> @interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate> @end @implementation ViewController{
CIContext *context;
CIFilter *filter;
CIImage *beginImage;
} - (void)viewDidLoad {
[super viewDidLoad]; //取得图片路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
NSURL *fileNameAndPath = [NSURL fileURLWithPath:filePath];
//根据路径,创建开始图片
beginImage = [CIImage imageWithContentsOfURL:fileNameAndPath]; context = [CIContext contextWithOptions:nil];
//创建滤镜,和输出图片
filter = [CIFilter filterWithName:@"CISepiaTone" keysAndValues: kCIInputImageKey, beginImage, @"inputIntensity", @0.8, nil];
CIImage *outputImage = [filter outputImage]; //在这里你用上下文对象里的一个方法来画一个CGImage,调用上下文中的createCGImage:fromRect:和提供的CIImage可以生成一个CGImageRef。
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]]; //下面,你用UIImage + imageWithCGImage,从CGImage中创建一个UIImage。
UIImage *newImage = [UIImage imageWithCGImage:cgimg];
self.imageView.image = newImage; //最后,开放 CGImageRef接口。 CGImage 是一个C接口,即使有ARC,也需要你自己来做内存管理。编译运行,确保正常工作。
CGImageRelease(cgimg); [self logAllFilters];
} - (IBAction)amountSliderValueChanged:(id)sender {
float slideValue = self.amountSlider.value;
[filter setValue:@(slideValue) forKey:@"inputIntensity"];
CIImage *outputImage = [filter outputImage];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImage = [UIImage imageWithCGImage:cgimg];
self.imageView.image = newImage;
CGImageRelease(cgimg);
} - (IBAction)loadPhoto:(id)sender {
UIImagePickerController *pickerC = [[UIImagePickerController alloc] init];
pickerC.delegate = self;
[self presentViewController:pickerC animated:YES completion:nil];
} - (IBAction)savePhoto:(id)sender {
CIImage *saveToSave = [filter outputImage];
CIContext *softwareContext = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer : @(YES)} ];
CGImageRef cgImg = [softwareContext createCGImage:saveToSave fromRect:[saveToSave extent]];
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:cgImg metadata:[saveToSave properties] completionBlock:^(NSURL *assetURL, NSError *error) { CGImageRelease(cgImg); }];
} - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *gotImage = [info objectForKey:UIImagePickerControllerOriginalImage];
beginImage = [CIImage imageWithCGImage:gotImage.CGImage];
[filter setValue:beginImage forKey:kCIInputImageKey];
[self amountSliderValueChanged:self.amountSlider];
} - (void)imagePickerControllerDidCancel: (UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
} /**
* 打印所有滤镜
*/
-(void)logAllFilters {
NSArray *properties = [CIFilter filterNamesInCategory: kCICategoryBuiltIn];
NSLog(@"%@", properties);
for (NSString *filterName in properties){
CIFilter *fltr = [CIFilter filterWithName:filterName];
NSLog(@"%@", [fltr attributes]);
}
} @end