iOS学习笔记OSiOS TipsiOS

iOS开发之滤镜的使用技巧

2016-03-26  本文已影响3132人  IOS_龙

一、滤镜的内容和效果是比较多并且复杂的 ,学习滤镜需要技巧 如下:

两个输出语句解决滤镜的属性选择问题:

二、了解滤镜的相关介绍

介绍

三、使用步骤

1.实例CIImage -> 先把UIImage -> CGImageRef -> CIImage
2.创建CIFilter滤镜并给滤镜设置属性(KVC)
3.创建CIContext上下文
4.初始化一个CGImageRef 输出图片对象 合并滤镜输出的图像
5.赋给UIImage对象进行显示
6.如果想使用滤镜链 可以再次添加效果

四、一个实例解析 滤镜 滤镜链 保存图片

代码示例:
#import "ViewController.h"//宏定义 屏幕的宽
#define SCREEN_WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)

    //注意挂上代理
    @interface ViewController ()    <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
    {

        UIImageView *myImageView;//接收图片的视图
        UIButton *photoButton;//从本地相册选择图片的按钮
        UIButton *filterButton;//添加滤镜的按钮
        UIButton *saveButton;//滤镜后保存到本地相册的按钮
    }
    @end
    @implementation ViewController
    - (void)viewDidLoad {
    [super viewDidLoad];
   //去除导航栏的高度
       self.edgesForExtendedLayout = UIRectEdgeNone;
   //设置背景色
       self.view.backgroundColor = [UIColor greenColor];

   //    创建按钮
       NSArray *titleButtonList = @[@"photo",@"Filter",@"save"];
       for (int i=0; i<titleButtonList.count; i++) {
           UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20+80*i, 20, 60, 40);
    [button setTitle:titleButtonList[i] forState:UIControlStateNormal];
    button.tag = 10 +i ;
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:button];
           }

       //    初始化图片视图
           myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, SCREEN_WIDTH-40, 300)];
           myImageView.backgroundColor = [UIColor cyanColor];
           [self.view addSubview:myImageView];

           photoButton = [self.view viewWithTag:10];
           filterButton = [self.view viewWithTag:11];
           saveButton = [self.view viewWithTag:12];

       //    给三个按钮添加触发事件
           [photoButton addTarget:self action:@selector(photoAction:) forControlEvents:UIControlEventTouchUpInside];
       //    滤镜按钮
           [filterButton addTarget:self action:@selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
       //保存滤镜后图片的按钮
           [saveButton addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];

       }
       //选择图片
       - (void)photoAction:(UIButton *)sender{
           UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
           pickerController.delegate = self;
           [self presentViewController:pickerController animated:YES completion:nil];
       }

       //把图片放在图片视图上
       - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
           UIImage *image = info[UIImagePickerControllerOriginalImage];
           myImageView.image = image;
           [self dismissViewControllerAnimated:YES completion:nil];
       }

       //滤镜按钮的触发方法
       - (void)filterAction:(UIButton *)sender{

       //    1.源图
           CIImage *inputImage = [CIImage imageWithCGImage:myImageView.image.CGImage];
       //    2.滤镜
           CIFilter *filter = [CIFilter filterWithName:@"CIColorMonochrome"];
       //    NSLog(@"%@",[CIFilter filterNamesInCategory:kCICategoryColorEffect]);//注意此处两个输出语句的重要作用
            NSLog(@"%@",filter.attributes);

           [filter setValue:inputImage forKey:kCIInputImageKey];

           [filter setValue:[CIColor colorWithRed:1.000 green:0.165 blue:0.176 alpha:1.000] forKey:kCIInputColorKey];
           CIImage *outImage = filter.outputImage;
           [self addFilterLinkerWithImage:outImage];

       }

       //再次添加滤镜  形成滤镜链
       - (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 resultImage = [context createCGImage:filter.outputImage fromRect:filter.outputImage.extent];
           myImageView.image = [UIImage imageWithCGImage:resultImage];

       }

       //保存滤镜后的图片到本地相册
       - (void)saveAction:(UIButton *)sender{
           UIImageWriteToSavedPhotosAlbum(myImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
       }

       //保存成功调用的方法
       - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
           NSLog(@"保存成功");
       }
       @end
上一篇下一篇

猜你喜欢

热点阅读