android ios动态模糊效果,iOS实现模糊效果的几种方法

iOS系统中大量使用了模糊效果,背景模糊,使得靠前的内容更容易获得用户的关注,自己开发中肯定也遇到过样的需求。今天就总结一下常用的实现模糊效果的方法,目录如下图:

本篇demo:iOS常用模糊效果demo

e683664a84bd

目录

1.UIImage+ImageEffects

这种办法实际上是对加载出的UIImage进行处理,得到模糊效果后,可以使用UIImage对象创建视图。

使用场景:需要对静态的图片进行模糊时

优点:

iOS7就可以使用;

官方代码,质量上有保证;

API简单易用;

可以控制参数,调节模糊效果;

e683664a84bd

效果

2.使用UIToolbar覆盖在要模糊内容的上方

使用场合:如果被模糊的内容不是静态内容,会在屏幕上动,那么只能使用这种办法。

优点:可以支持较低的系统版本

缺点:不可调节模糊效果,并且与第一种方法的效果并不一致

//关键代码

self.toolbar = [[UIToolbar alloc] initWithFrame: self.view.bounds];

self.toolbar.barStyle= UIBarStyleDefault;//UIBarStyleBlack

[imageView addSubview: self.toolbar];

e683664a84bd

效果

3.使用UIVisualEffectView

在iOS8中,苹果引入了UIVisualEffectView以及UIVisualEffect,利用这些新的特性,非常容易添加两种效果: UIBlurEffect(高斯模糊)以及UIVibrancyEffect(这种效果能把当前视图和背景视图混合起来)。

优点:苹果原生支持

缺点:iOS8及以上的系统可使用;不支持调节参数。

//Blur effect 模糊效果

UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle: UIBlurEffectStyleLight];

UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect: blurEffect];

blurView.frame = self.view.bounds;

[self.bgImageView addSubview: blurView];

//无生动效果

UIImageView *withoutVibrancy = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"minIcon"]];

withoutVibrancy.frame = CGRectMake(50, 150, 40, 40);

[self.bgImageView addSubview: withoutVibrancy];

UILabel *withouttitle = [[UILabel alloc]init];

withouttitle.frame = CGRectMake(100, 150, 150, 40);

withouttitle.text = @"无生动效果~~";

withouttitle.textColor = [UIColor whiteColor];

[self.bgImageView addSubview:withouttitle];

//有生动效果

UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blurEffect];

UIVisualEffectView *vibrancyView = [[UIVisualEffectView alloc] initWithEffect: vibrancyEffect];

vibrancyView.frame = blurView.bounds;

[blurView.contentView addSubview: vibrancyView];

UIImageView *iconView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"minIcon"]];

iconView.frame = CGRectMake(50, 250, 40, 40);

[vibrancyView.contentView addSubview: iconView];/必须加到vibrancyView的contentView上

UILabel *title = [[UILabel alloc]init];

title.frame = CGRectMake(100, 250, 150, 40);

title.text = @"有生动效果~~";

title.textColor = [UIColor whiteColor];

[vibrancyView.contentView addSubview:title];//必须加到vibrancyView的contentView上

e683664a84bd

效果

4.CoreImage

Core Image 是苹果用来简化图片处理的框架,在 iOS 平台上,5.0 之后就出现了 Core Image 的 API。Core Image 的 API 被放在 CoreImage.framework 库中。不过直到iOS6.0才开始支持模糊。这个API调用起来很方便简洁。

在 iOS 和 OS X 平台上,Core Image 都提供了大量的滤镜(Filter),这也是 Core Image 库中比较核心的东西之一。按照官方文档记载,在 OS X 上有 120 多种 Filter,而在 iOS 上也有 90 多种。

优点: 模糊效果较好,模糊程度的可调范围很大,可以根据实际的需求随意调试。

缺点: 耗时,需要导入 #import

/* 0.导入CIImage图片 */

UIImage *sourceImage = [UIImage imageNamed:@"bgView.jpg"];

CIImage *ciImage = [CIImage imageWithCGImage:[sourceImage CGImage]];

/* 1.创建出Filter滤镜 */

//过滤器

CIFilter *blurFilter = [CIFilter filterWithName:self.filterName];

//将图片输入到滤镜中

[blurFilter setValue:ciImage forKey:kCIInputImageKey];

//设置模糊程度

[gaussianBlur setValue:[NSNumber numberWithFloat:_slider.value*20] forKey:@"inputRadius"];

//将处理之后的图片输出

CIImage *outCIImage = [blurFilter valueForKey:kCIOutputImageKey];

/* 2.用CIContext将滤镜中的图片渲染出来 */

/* 获取CGImage句柄

* createCGImage: 处理过的CIImage

* fromRect: 如果从处理过的图片获取frame会比原图小, 因此在此需要设置为原始的CIImage.frame

*/

CIContext *context = [CIContext contextWithOptions:nil];

CGImageRef outCGImageRef = [context createCGImage:outCIImage fromRect:[ciImage extent]];

/* 3.导出图片 */

//获取到最终图片

UIImage *resultImage = [UIImage imageWithCGImage:outCGImageRef];

//释放句柄

CGImageRelease(outCGImageRef);

/* 4.加载图片 */

self.imageView.image = resultImage;

e683664a84bd

效果

5.FXblurView

FXblurView是一个UIView的子类,效果和iOS7的背景实时模糊效果一样,但是支持到了 iOS 5.0。

FXBlurView 有两种模式,一种是 static 静态模糊:也就是只模糊一次,后面即使背景图片变化了,模糊效果也不会变化;另外就是 dynamic 动态模糊:这会实时的对背景图片进行模糊,是会不断变化的。

self.fxView = [[FXBlurView alloc] initWithFrame:CGRectMake(0, 0, self.bgImageView.frame.size.width, self.bgImageView.frame.size.height)];

//动态

self.fxView.dynamic = NO;

//模糊范围

self.fxView.blurRadius = 20;

//背景色

self.fxView.tintColor = [UIColor clearColor];

[self.bgImageView addSubview:self.fxView];

e683664a84bd

效果

写在后面:GPUImage也可以实现模糊效果,它是用设备的GPU来实时处理图片,给图片视频加各种滤镜效果的一个开源库, 太牛逼的一个框架, 感觉用在这里有些大材小用了,鉴于篇幅以后有时间单独写一篇关于GPUImage的吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值