iOS动画图层 — iOS模糊效果

     在iOS平台下,如果需要对UIImageView中的图片进行简单的模糊处理,可以使用自带的API进行处理。

     CoreImage是苹果用来简化图片处理的框架。

     首先我们将一张图片显示到UIImageView中,效果如下:

     

     下面的代码片段可以实现图片的模糊效果

	//CIImage
	CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"bg"]];
	//CIFilter 设置一个模糊滤镜
	CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
	//将图片放入滤镜中
	[blurFilter setValue:ciImage forKey:kCIInputImageKey];
	//输出CIFilter的键值对描述
	NSLog(@"%@",[blurFilter attributes]);
	//设置模糊程度
	[blurFilter setValue:@(2) forKey:@"inputRadius"];
	//将处理好的图片输出
	CIImage *outimage = [blurFilter valueForKey:kCIOutputImageKey];
	//CIContext 设置nil的时候默认使用CPU渲染
	CIContext *context = [CIContext contextWithOptions:nil];
	//获取CIImage句柄
	CGImageRef outputCGImage = [context createCGImage:outimage fromRect:[outimage extent]];
	//最终获取图片
	UIImage *blurImage = [UIImage imageWithCGImage:outputCGImage];
	//释放CGImage句柄
	CGImageRelease(outputCGImage);
<pre name="code" class="objc">

 
 

     现在将blurImage显示的效果就变成下图:

     

     如果需要对图片上添加的文本进行模糊渲染,可以使用UIVisualEffectView进行实时渲染,但是UIVisualEffectView只能够在iOS8及以后的版本使用,因此,如果你的App需要兼容以前版本,还是需要考虑一下。

     另外需要注意的是,UIVisualEffectView需要显示在UIScrollView上。我们需要达到的效果是如下这样:

     

     下面贴出代码片段:

      

#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 * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg"]];
    self.scrollView.contentSize = imageView.image.size;
    [self.scrollView addSubview:imageView];
    [self.view addSubview:self.scrollView];
	
    //添加模糊效果(这块部分是我们截图中白色的模糊背景)
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
    //设置effectView的frame值
    effectView.frame = CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 50);
    [self.view addSubview:effectView];
	
    //添加显示文本
    //初始化label并且设置frame值
    UILabel *label = [[UILabel alloc] initWithFrame:effectView.bounds];
    //label的text值
    label.text = @"黎明中的城市";
    //label的字体大小
    label.font = [UIFont systemFontOfSize:30];
    //label的对齐方式
    label.textAlignment = NSTextAlignmentCenter;
    [effectView.contentView addSubview:label];
	
    //添加模糊子view的UIVisualEffectView
    /*创建出子模糊view,需要注意的是,这里的subEffectView相当于是对label的模糊处理,label的模糊
      效果需要和effectView的模糊效果保持一致,因此我们使用了[UIVibrancyEffect effectForBlurEf      fect(UIBlurEffect *)effectView.effect]
     */
    UIVisualEffectView *subEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)effectView.effect]];
	
    subEffectView.frame = effectView.bounds;
    //将子模糊view添加到effectView的contentView中才能生效
    [effectView.contentView addSubview:subEffectView];
    //添加要显示的view来达到特殊效果
    [subEffectView.contentView addSubview:label];
	
}
@end


 
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值