当前位置:   article > 正文

iOS中滤镜的使用(一) 马赛克滤镜_ios cipixellate demo

ios cipixellate demo

iOS中滤镜的使用(一)

马赛克滤镜



首先 要加载图片并转化为CIImage

CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"IMG_0160"]];
  • 1

其次,创建filter滤镜

CIFilter *filter = [CIFilter filterWithName:@"CIPixellate"];
NSLog(@"%@",filter.attributes);
[filter setValue:ciImage  forKey:kCIInputImageKey];
[filter setDefaults];
CIImage *outImage = [filter           valueForKey:kCIOutputImageKey];
  • 1
  • 2
  • 3
  • 4
  • 5

然后,用CIContext将滤镜中的图片渲染出来,原因,导出图片过过程中用到CGImageRef。

CIContext *context = [CIContext contextWithOptions:nil];

CGImageRef cgImage = [context createCGImage:outImage fromRect:[outImage extent]];
  • 1
  • 2
  • 3

最后,导出图片,注意内存的释放

 UIImage *showImage = [UIImage imageWithCGImage:cgImage];

CGImageRelease(cgImage);
  • 1
  • 2
  • 3

剩下的工作就是加载马赛克图片了

 UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 600, 400)];
imageView.image = showImage;
imageView.center = self.view.center;
[self.view addSubview:imageView];
  • 1
  • 2
  • 3
  • 4

而Filter的属性有如下:
这里写图片描述

总的代码如下:

 //0:导入图片

    CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"IMG_0160"]];

    //1:创建filter滤镜

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

    NSLog(@"%@",filter.attributes);
    [filter setValue:ciImage  forKey:kCIInputImageKey];

    [filter setDefaults];

    CIImage *outImage = [filter valueForKey:kCIOutputImageKey];

    //2:用CIContext将滤镜中的图片渲染出来

    CIContext *context = [CIContext contextWithOptions:nil];

    CGImageRef cgImage = [context createCGImage:outImage fromRect:[outImage extent]];

    //3:导出图片

    UIImage *showImage = [UIImage imageWithCGImage:cgImage];

    CGImageRelease(cgImage);

    //4:加载出来

    UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 600, 400)];
    imageView.image = showImage;
    imageView.center = self.view.center;
    [self.view addSubview:imageView];
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/125028
推荐阅读
相关标签
  

闽ICP备14008679号