首先看原圖:
![Uploading 屏幕快照 2017-06-04 下午3.50.00_754738.png . . .]
主要有三種實現(xiàn)方式:
1.UIToolBar:
//只需要在想要此效果的地方加上toobar即可實現(xiàn)
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height)];
toolbar.barStyle = UIBarStyleBlack;
toolbar.translucent = YES;
[self.imageView addSubview:toolbar];
屏幕快照 2017-06-04 下午3.50.00.png
2.UIBlurEffect:
在 iOS8.0 之后均牢,蘋果新增了一個類 UIVisualEffectView徘跪,基本使用和UIToolBar類似,需要注意的是松邪,UIVisualEffectView 是一個抽象類哨查,不能直接使用,需通過其子類UIBlurEffect,UIVibrancyEffect來實現(xiàn)
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
effectView.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);
[self.imageView addSubview:effectView];
屏幕快照 2017-06-04 下午3.51.02.png
UIVibrancyEffect:生動的效果
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVibrancyEffect *effect2 = [UIVibrancyEffect effectForBlurEffect:effect];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect2];
effectView.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);
//通過改變contentView中subView锋八,可以實現(xiàn)不同更加生動的效果
UIView *yellowView = [[UIView alloc] init];
yellowView.backgroundColor = [UIColor yellowColor];
yellowView.frame = effectView.frame;
[effectView.contentView addSubview:yellowView];
[self.imageView addSubview:effectView];
屏幕快照 2017-06-04 下午3.50.13.png
如果使用
yellowView.backgroundColor = [UIColor yellowColor];
效果如下:
屏幕快照 2017-06-04 下午3.57.40.png
3.第三方庫:
LBBlurredImage 方式實現(xiàn)
//需要#import "UIImage+ImageEffects.h"
self.imageView.image = [[UIImage imageNamed:@"1"] applyDarkEffect];
屏幕快照 2017-06-04 下午3.50.27.png
而使用該庫給出的方法:
[self.imageView setImageToBlur:self.imageView.image completionBlock:^{
NSLog(@"The LBBlurred image has been set");
}];
會發(fā)現(xiàn)圖片會有一瞬間是原圖,這是因為
屏幕快照 2017-06-04 下午3.18.07.png
該方法使用了異步隊列紊服,然后進(jìn)行圖片的重繪,重繪完成之后才在主隊列更新参萄,如果不想要這個閃一下的效果煎饼,可以去掉異步,都在主隊列就沒問題了筒溃。
Demo參考地址:https://github.com/CSRDemo/UIBlurEffectDemo
參考文章:http://www.cocoachina.com/ios/20170531/19392.html
歡迎留言探討沾乘。