您的位置:首页 > 移动开发 > IOS开发

IOS实现图片的模糊效果

2016-06-20 18:19 477 查看
模糊效果前:           模糊效果后:


                         

 

//

//  ViewController.m

//  BlurImage

//

//  Created by Luck on 16/6/20.

//  Copyright © 2016年 hongmw. All rights reserved.

//

#import "ViewController.h"

@interface
ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    UIImage * img           = [UIImageimageNamed:@"03"];

    

    //CIImage

    CIImage * ciImage       = [[CIImagealloc]initWithImage:img];

    

   
//过滤器(滤镜)

    CIFilter * filter       = [CIFilterfilterWithName:@"CIGaussianBlur"];

    

    //KVC
添加到滤镜中

    [filter setValue:ciImage
forKey:kCIInputImageKey];

    

   //设置模糊程度

    [filter setValue:@(50)
forKey:@"inputRadius"];

    

   
//输出看看滤镜还可以设置的参数

    NSLog(@"%@",[filter
attributes]);

   
//获取处理好的图片

    CIImage * blurImg       = [filter
valueForKey:kCIOutputImageKey];

    

    //默认就是CPU渲染的  
如果想要GPU
渲染参数不能为nil

    CIContext * context = [CIContextcontextWithOptions:nil];

    

    //获取到 CGImageRef句柄

    CGImageRef imgRef = [context
createCGImage:blurImg fromRect:[blurImgextent]];

    

   
//最终模糊效果的图片

    UIImage * outImg    = [UIImageimageWithCGImage:imgRef];

    

    //280 × 300

    UIImageView * imgView   = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,
280, 300)];

    imgView.image           = outImg;

    imgView.center          =
self.view.center;

    [self.viewaddSubview:imgView];

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: