在 iOS 8 中使用模糊效果

2022-05-30 22:06:09 字數 1574 閱讀 2926

從 sample code 中可以看出來, 實作上利用三次 box blur 來接近高斯模糊的效果,速度上也比較快。相關的原理可以看這個鏈結:  filter primitive 『fegaussianblur』

我有把這兩個實作主要的檔案抽出來成為乙個 pod ,可以在 podfile 加入下方程式碼後執行pod install: 

pod 'uiimageblureffectcategory', :git => ''

接著引入 header file :

#import 

我把 code 改成使用uiimagecategory ,以提供擴充 class methods 的方式來使用。舉例來說,如果要產生乙個簡單模糊過的影象,就用以下的方式即可: 

原先提供的 sample code 提供了比較大的變化空間,有比較多的引數( blur radius, tint color, saturation delta factor 以及 mask image )可以調整模糊的效果,到 ios 8 還是可以使用。

在 ios 8 之後, uikit 則提供了簡單的方式來達成模糊的效果,簡單幾個步驟就可以直接模糊 uiview instances 。

objective-c

uivisualeffect *blureffect= [uiblureffect effectwithstyle:uiblureffectstylelight];

uivisualeffectview *visualeffectview= [[uivisualeffectview alloc] initwitheffect:blureffect];

visualeffectview.frame = imageview.bounds

[imageview addsubview:visualeffectview];

swift

var visualeffectview = uivisualeffectview(effect: uiblureffect(style: .light)) as uivisualeffectview

visualeffectview.frame = imageview.bounds

imageview.addsubview(visualeffectview)

uiblureffect 的種類

uiblureffect 的種類則有以下幾種可以使用:

以下是分別的效果展示,前景加上原圖對照:

原先使用 vimage 的 code 對想要對效果做客製化比較容易達成。而 ios 8 新增的這個 visual effect 則是提供了最容易的達成方式,怎麼用就端看需求了。

而為了看懂 sample code 也稍微看了一些資料,和一些以前沒接觸過的運算公式,需要花了一些時間消化這些東西。不過花點時間了解他背後是怎麼做到的,也滿有收穫的。

iOS 8中使用CoreLocation定位

1 在使用corelocation前需要呼叫如下函式 ios 8專用 ios 8對定位進行了一些修改,其中包括定位授權的方法,cllocationmanager增加了下面的兩個方法 1 始終允許訪問位置資訊 void requestalwaysauthorization 2 使用應用程式期間允許訪問...

iOS8中使用CoreLocation定位 轉

本文 1 首先定義乙個全域性的變數用來記錄cllocationmanager物件,引入corelocation.framework使用 import property nonatomic,strong cllocationmanager locationmanager 2 初始化cllocation...

CoreLocation在iOS8上用法的變化

1 在使用corelocation前需要呼叫如下函式 ios8專用 ios8對定位進行了一些修改,其中包括定位授權的方法,cllocationmanager增加了下面的兩個方法 1 始終允許訪問位置資訊 void requestalwaysauthorization 2 使用應用程式期間允許訪問位置...