十行代碼實現微信馬賽克濾鏡效果

昨天下午微信的朋友圈著實火了一把,在這之后好多程序員都通過抓包工具看到了原圖,但是我卻在想,網上說是在移動前端做到的那是怎么做到的呢,經過一些學習,終于掌握了一些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]);
        
    }
    
}

同學們可以自行學習并搭配使用哦。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,540評論 25 708
  • 從最壞的角度去衡量 無論做什么事件,要先把最壞的結果想到。這個能有效降低對事件成功狂熱的期望值,能有效降低失...
    次第花開1583閱讀 656評論 0 0
  • 從小到大,由于生活在一個普通的工薪家庭,從來沒有怎么在意過“儀式感”這樣抽象的概念。 在從前的我看來,所謂的過年,...
    非彼恒閱讀 4,279評論 10 10