iOS模糊效果的實(shí)現(xiàn)方法
本文實(shí)例為大家分享了iOS模糊效果的3種方法,供大家參考,具體內(nèi)容如下
方案一:利用系統(tǒng)的CoreImage(濾鏡)
重點(diǎn)理解CIImage,CIFilter,CIContext,CGImageRef
濾鏡處理的過程比較慢,會(huì)造成加載圖片緩慢的現(xiàn)象(等一會(huì)才看到圖片),盡量放到子線程執(zhí)行
- (void)viewDidLoad {
[super viewDidLoad];
// 加載一張圖片
UIImage *image = [UIImage imageNamed:@"che"];
/**************CoreImage部分**************/
// 1.創(chuàng)建CIImage
CIImage *ciImage = [[CIImage alloc] initWithImage:image];
// 2.創(chuàng)建濾鏡CIFilter
CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
// 2.1.將CIImage輸入到濾鏡中
[blurFilter setValue:ciImage forKey:kCIInputImageKey];
// 可以通過該方法查看我們可以設(shè)置的值(如模糊度等)
NSLog(@"%@", [blurFilter attributes]);
// 2.2設(shè)置模糊度
[blurFilter setValue:@(2) forKey:@"inputRadius"];
// 2.3將處理好的圖片輸出
CIImage *outCiImage = [blurFilter valueForKey:kCIOutputImageKey];
// 3.CIContext(option參數(shù)為nil代表用CPU渲染,若想用GPU渲染請(qǐng)查看此參數(shù))
CIContext *context = [CIContext contextWithOptions:nil];
// 4.獲取CGImage句柄
CGImageRef outCGImage = [context createCGImage:outCiImage fromRect:[outCiImage extent]];
// 5.獲取最終的圖片
UIImage *blurImage = [UIImage imageWithCGImage:outCGImage];
// 6.釋放CGImage
CGImageRelease(outCGImage);
/*****************************************/
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 750 / 2, 1334 / 2)];
imageV.image = blurImage;
imageV.center = self.view.center;
[self.view addSubview:imageV];
}
方案二:利用UIImage+ImageEffects分類
將UIImage+ImageEffects.h和UIImage+ImageEffects.m文件加載進(jìn)工程
包含UIImage+ImageEffects.h
UIImage+ImageEffects文件路徑
#import "ViewController.h"
#import "UIImage+ImageEffects.h"
- (void)viewDidLoad {
[super viewDidLoad];
// 原始圖片
UIImage *sourceImage = [UIImage imageNamed:@"che"];
// 對(duì)圖片進(jìn)行模糊處理
UIImage *blurImage = [sourceImage blurImageWithRadius:10];
// 加載模糊處理后的圖片
UIImageView *imageV = [[UIImageView alloc] initWithImage:blurImage];
[self.view addSubview:imageV];
}
方案三:利用UIVisualEffectView(iOS8)
#import "ViewController.h"
@interface ViewController ()
/** 背景 */
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 添加展示的背景,用于顯示動(dòng)態(tài)模糊(背景能夠滾動(dòng),便于查看動(dòng)態(tài)的模糊)
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.創(chuàng)建模糊view
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
// 2.設(shè)定模糊View的尺寸
effectView.frame = CGRectMake(0, 100, 375, 200);
// 3.添加到view當(dāng)中
[self.view addSubview:effectView];
/******************添加顯示文本******************/
UILabel *label = [[UILabel alloc] initWithFrame:effectView.bounds];
label.text = @"模糊效果";
label.font = [UIFont systemFontOfSize:40];
label.textAlignment = NSTextAlignmentCenter;
/****************添加模糊效果的子view****************/
// 1.創(chuàng)建出子模糊view
UIVisualEffectView *subEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)effectView.effect]];
// 2.設(shè)置子模糊view的尺寸
subEffectView.frame = effectView.bounds;
// 3.將子模糊view添加到effectView的contentView上才能顯示
[effectView.contentView addSubview:subEffectView];
// 4.添加要顯示的view來(lái)達(dá)到特殊效果
[subEffectView.contentView addSubview:label];
}
@end
效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS Object-C 中Runtime詳解及實(shí)例代碼
這篇文章主要介紹了IOS Object-C 中Runtime詳解及實(shí)例代碼的相關(guān)資料,OC中的對(duì)象其實(shí)在Runtime中都會(huì)用結(jié)構(gòu)體來(lái)表示,這個(gè)結(jié)構(gòu)體中包含了類名、成員變量列表、方法列表、協(xié)議列表、緩存等,需要的朋友可以參考下2017-03-03
iOS逆向工程使用dumpdecrypted工具給App脫殼
這篇文章主要介紹了iOS逆向工程使用dumpdecrypted工具給App脫殼的相關(guān)資料,本文圖文并茂給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
iOS11 下載之?dāng)帱c(diǎn)續(xù)傳的bug的解決方法
本篇文章主要介紹了iOS11 下載之?dāng)帱c(diǎn)續(xù)傳的bug的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-11-11
Objective-C關(guān)鍵字@property使用原理探究
這篇文章主要為大家介紹了Objective-C關(guān)鍵字@property使用原理探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
iOS應(yīng)用開發(fā)中UIScrollView滾動(dòng)視圖的基本用法總結(jié)
這篇文章主要介紹了iOS應(yīng)用開發(fā)中UIScrollView滾動(dòng)視圖的基本用法總結(jié),作者還介紹了重寫UIScrollView中的hitTest方法來(lái)解決長(zhǎng)按的事件問題,需要的朋友可以參考下2016-02-02
IOS 靜態(tài)方法與動(dòng)態(tài)方法詳解
這篇文章主要介紹了IOS 靜態(tài)方法與動(dòng)態(tài)方法詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02
iOS 防止按鈕多次點(diǎn)擊造成多次響應(yīng)的方法
這篇文章主要介紹了iOS 防止按鈕多次點(diǎn)擊造成多次響應(yīng)的方法的相關(guān)資料,這里對(duì)多次點(diǎn)擊造成的響應(yīng)提供了解決辦法,需要的朋友可以參考下2016-11-11

