赞
踩
首先 要加载图片并转化为CIImage
CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"IMG_0160"]];
其次,创建filter滤镜
CIFilter *filter = [CIFilter filterWithName:@"CIPixellate"];
NSLog(@"%@",filter.attributes);
[filter setValue:ciImage forKey:kCIInputImageKey];
[filter setDefaults];
CIImage *outImage = [filter valueForKey:kCIOutputImageKey];
然后,用CIContext将滤镜中的图片渲染出来,原因,导出图片过过程中用到CGImageRef。
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:outImage fromRect:[outImage extent]];
最后,导出图片,注意内存的释放
UIImage *showImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
剩下的工作就是加载马赛克图片了
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 600, 400)];
imageView.image = showImage;
imageView.center = self.view.center;
[self.view addSubview:imageView];
而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];
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。