十行代码实现微信马赛克滤镜效果

昨天下午微信的朋友圈着实火了一把,在这之后好多程序员都通过抓包工具看到了原图,但是我却在想,网上说是在移动前端做到的那是怎么做到的呢,经过一些学习,终于掌握了一些Core Image的知识,做出了相应的效果,仅仅十行代码

 UIImageView * imgView = [[UIImageView alloc]init];
    imgView.frame = CGRectMake(50, 50, 200, 200);
    [self.view addSubview:imgView];
    UIImage * img = [UIImage imageNamed:@"Result.png"];
    CIImage * inputImg = [CIImage imageWithCGImage:img.CGImage];
    CIContext * context = [CIContext contextWithOptions:nil];
    //在这里设置Core Image的相应效果
    CIFilter * filter = [CIFilter filterWithName:@"CIGaussianBlur" keysAndValues:kCIInputImageKey,inputImg,@"inputRadius",@20, nil];
    CIImage * outPutImg = [filter outputImage];
    CGImageRef outImage = [context createCGImage:outPutImg fromRect:[outPutImg extent]];
    UIImage * newImg = [UIImage imageWithCGImage:outImage];
    NSLog(@"%@",newImg);
    imgView.image = newImg;

上述仅仅是Core Image的一种效果,而Core Image的效果有上百种,要想熟练使用还需要进一步的学习,
提供下面的一种找到全部效果的方法

-(void)showAllFilter
{
    NSArray * filterNames = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
    for(NSString * filterName in filterNames)
    {
        CIFilter * filter  = [CIFilter filterWithName:filterName];
        NSLog(@"\r filter:%@ \rattributes:%@",filterName,[filter attributes]);
        
    }
    
}

同学们可以自行学习并搭配使用哦。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,856评论 25 709
  • 从最坏的角度去衡量 无论做什么事件,要先把最坏的结果想到。这个能有效降低对事件成功狂热的期望值,能有效降低失...
    次第花开1583阅读 3,878评论 0 0
  • 从小到大,由于生活在一个普通的工薪家庭,从来没有怎么在意过“仪式感”这样抽象的概念。 在从前的我看来,所谓的过年,...
    非彼恒阅读 9,690评论 10 10