关闭

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

标签: iosuiimage滤镜Filter
854人阅读 评论(0) 收藏 举报
分类:

iOS中滤镜的使用(一)

马赛克滤镜



首先 要加载图片并转化为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];
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24209次
    • 积分:473
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:14篇
    • 译文:0篇
    • 评论:0条