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
//
// 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
相关文章推荐
- iOS下WebRTC音视频通话(三)-音视频通话
- iOS下WebRTC音视频通话(三)-音视频通话
- iOS 编译包含 bitcode 的 opencore-amr-iOS 静态库
- 第三章:iOS的数据存储与IO
- JSPatch – 动态更新iOS APP
- ios开发手机尺寸
- JSPatch – 动态更新iOS APP
- iOS 获取当前时间
- 一个彩票的框架demo
- ios开发——AirPlay的琢磨经历
- ios 学习之 NSPredicate 模糊、精确、查询
- iOS_二维码名片vCard
- AsyncSocket
- 多个Target的podfile文件配置
- iOS FMDB自己封装的单例类
- iOS 更新xcode后weak问题的解决方法
- IOS 接入环信SDK3.0时遇到的问题
- iOS开发之AsyncSocket使用教程
- ios url上加cooking
- iOS小技巧22-MacOS 苹果系统下使用MailDrop发送附件(大于20M)