图片模糊
Posted 孤独的菜鸟----
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片模糊相关的知识,希望对你有一定的参考价值。
UIImage, CGImageRef, CIImage区别
http://www.jianshu.com/p/aa2557d977ba
1. CoreImage图片模糊
UIImage *image = [UIImage imageNamed:@"sj_choujiangBG.jpg"];
/** 模糊处理 coreImage **/
// 输入图片
CIImage *inputCIImage = [[CIImage alloc] initWithImage:image];
// 滤镜设置
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:inputCIImage forKey:kCIInputImageKey];
// 输出图片
CIImage *outCIImage = [filter valueForKey:kCIOutputImageKey];
// CIImage 对象不可更改,是线程安全
// CIContext *context = [CIContext contextWithOptions:nil];
// CGImageRef outCGImage = [context createCGImage:outCIImage fromRect:[outCIImage extent]];
// UIImage *finalImage = [UIImage imageWithCGImage:outCGImage];
// 最终图片
UIImage *finalImage = [UIImage imageWithCIImage:outCIImage];
/** 模糊图片很耗时间,最好放到子线程中 **/
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.image = finalImage;
[self.view addSubview:imageView];
2.利用UIImage+ImageEffects分类
原码网址:https://github.com/YouXianMing/UIImageBlur
// 导入文件:UIImage+ImageEffects.h和UIImage+ImageEffects.m
//1.导入头文件:#import"UIImage+ImageEffects.h"
//原始图片
UIImage *sourceImage = [UIImage imageNamed:@"normal"];
//对图片进行处理(图片渲染用时,应在子线程中,此处未做此处理)
UIImage *blurImage = [sourceImage blurImageAtFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height)];//设置frame可进行局部模糊
//加载图片
UIImageView *imageView = [[UIImageView alloc]initWithImage:blurImage];
[self.view addSubview:imageView];
3.UIVisualEffectView(ios8)
#import "ViewController.h"
@interface ViewController ()
/** 背景 */
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
// 添加展示的背景,用于显示动态模糊(背景能够滚动,便于查看动态的模糊)
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fengjing"]];
self.scrollView.contentSize = imageV.image.size;
self.scrollView.bounces = NO;
[self.scrollView addSubview:imageV];
[self.view addSubview:self.scrollView];
/***************添加模糊效果***************/
// 1.创建模糊view
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
// 2.设定模糊View的尺寸
effectView.frame = CGRectMake(0, 100, 375, 200);
// 3.添加到view当中
[self.view addSubview:effectView];
/******************添加显示文本******************/
UILabel *label = [[UILabel alloc] initWithFrame:effectView.bounds];
label.text = @"模糊效果";
label.font = [UIFont systemFontOfSize:40];
label.textAlignment = NSTextAlignmentCenter;
/****************添加模糊效果的子view****************/
// 1.创建出子模糊view
UIVisualEffectView *subEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)effectView.effect]];
// 2.设置子模糊view的尺寸
subEffectView.frame = effectView.bounds;
// 3.将子模糊view添加到effectView的contentView上才能显示
[effectView.contentView addSubview:subEffectView];
// 4.添加要显示的view来达到特殊效果
[subEffectView.contentView addSubview:label];
@end
以上是关于图片模糊的主要内容,如果未能解决你的问题,请参考以下文章