zoukankan      html  css  js  c++  java
  • CoreImage 处理图片

    1.CoreImage 滤镜的使用(马赛克模糊)

    CoreImage是苹果公司为了简化图片处理的难度而开发出来的类库。

    随着iOS版本号升级以及硬件性能的不断提升,CoreImage将支持越来越多的滤镜

    - (void)hFilter {

        // 0. 导入CIImage图片

        CIImage *ciImage = [[CIImagealloc] initWithImage:[UIImageimageNamed:@"demo"]];

        

        // 1. 创建出Filter滤镜

        CIFilter *filter = [CIFilterfilterWithName:@"CIPixellate"];

        

        [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];

    }

                             

             


  • 相关阅读:
    go学习笔记day01
    go学习笔记day02
    SQLSERVER SQL性能优化
    Javascript面向对象编程(二):构造函数的继承
    Net 下采用GET/POST/SOAP方式动态调用WebService
    Asp.Net下采用GET/POST/SOAP方式动态调用WebService
    crontab用法(例子)
    Javascript面向对象编程(三):非构造函数的继承
    学习Javascript闭包(Closure)
    js 模拟队列类
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5411354.html
Copyright © 2011-2022 走看看