zoukankan      html  css  js  c++  java
  • IOS图像处理(9)使用CoreImage滤镜

    CoreImage是IOS5中新增框架,通过这个框架我们可以轻松地对图片进行各种特效处理

    使用CoreImage的主要流程如下(需要添加CoreImage框架至项目中):

    1 创建CIContext,IOS系统提供了3种方法创建CIContext

    a.基于CPU

    CIContext *context = [CIContext contextWithOption:@{kCIContextUseSoftwareRenderer:@YES}];

    b.基于GPU

    CIContext *context = [CIContext contextWithOption:nil];

    最常用也是性能较好地一种方式,缺点是基于GPU的CIContext无法跨应用访问,比如打开UIImagePickerController选取图片,如果直接在委托方法中使用CIContext,将会自动降为基于CPU,所以可以在委托方法中保存图片,回到主类中调用CIContext进行处理

    c.基于OpenGL优化,可获得实时性能

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

    CIContext *context = [CIContext contextWithEAGLContext:eaglContext];

    2 创建CIFilter过滤器

    3 创建CIImage图片作为源图片,可以使用NSURL,UIImage以及NSData创建

    4 将CIImage设置为CIFilter的inputImage,同时根据不同的过滤器设置不同的参数

    5 通过CIFilter获取进过过滤器转换后的图片,图片格式为CIImage,通过CIContext可以得到CGImage

    以下例子展示了像素画的过滤器

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        //可以打印出所有的过滤器以及支持的属性
        NSArray *filters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
        for (NSString *filterName in filters) {
            CIFilter *filter = [CIFilter filterWithName:filterName];
            NSLog(@"%@,%@",filterName,[filter attributes]);
        }
        
        
        //创建基于GPU的CIContext
        CIContext *context = [CIContext contextWithOptions:nil];
        
        //创建过滤器
        CIFilter *filter = [CIFilter filterWithName:@"CIPixellate"];
        
        //创建CIImage
        CIImage *sourceImage = [CIImage imageWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"girl" ofType:@"jpg"]]];
        
        //将CIImage设为源图片
        [filter setValue:sourceImage forKey:@"inputImage"];
        
        //设置过滤参数(像素大小)
        [filter setValue:@15 forKey:@"inputScale"];
        
        //得到输出图片
        CIImage *outputImage = [filter outputImage];
        
        CGImageRef cgImage= [context createCGImage:outputImage fromRect:[UIScreen mainScreen].bounds];
        UIImage *image = [UIImage imageWithCGImage:cgImage];
        
        //调用了create创建,需要release
        CGImageRelease(cgImage);
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        [self.view addSubview:imageView];
        
    } 

  • 相关阅读:
    P3371 【模板】单源最短路径(弱化版)
    村村通
    P1551 亲戚题解
    P4467 [SCOI2007]k短路
    P2483 【模板】k短路([SDOI2010]魔法猪学院)
    POJ——2449 Remmarguts' Date
    P1337 [JSOI2004]平衡点 / 吊打XXX
    P1118 [USACO06FEB]数字三角形`Backward Digit Su`…
    P1621 集合
    P1514 引水入城
  • 原文地址:https://www.cnblogs.com/zanglitao/p/4038359.html
Copyright © 2011-2022 走看看