iOS給圖片添加濾鏡&使用openGLES動態(tài)渲染圖片詳解及實(shí)例
iOS給圖片添加濾鏡&使用openGLES動態(tài)渲染圖片
給圖片增加濾鏡有這兩種方式: CoreImage / openGLES
下面先說明如何使用CoreImage給圖片添加濾鏡, 主要為以下步驟:
#1.導(dǎo)入CIImage格式的原始圖片
#2.創(chuàng)建CIFilter濾鏡
#3.用CIContext將濾鏡中的圖片渲染出來
#4.導(dǎo)出渲染后的圖片
參考代碼:
//導(dǎo)入CIImage CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"hua"]]; //創(chuàng)建出Filter濾鏡 CIFilter *filter = [CIFilter filterWithName:@"CIPixellate"]; [filter setValue:ciImage forKey:kCIInputImageKey]; [filter setDefaults]; CIImage *outImage = [filter valueForKey:kCIOutputImageKey]; //用CIContext將濾鏡中的圖片渲染出來 CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef cgImage = [context createCGImage:outImage fromRect:[outImage extent]]; //導(dǎo)出圖片 UIImage *showImage = [UIImage imageWithCGImage:cgImage]; CGImageRelease(cgImage); UIImageView *imageView = [[UIImageView alloc] initWithImage:showImage]; imageView.center = self.view.center; [self.view addSubview:imageView];
當(dāng)要設(shè)置多個濾鏡的時候, 出了新創(chuàng)建一個CIFilter外還要額外設(shè)定kCIInputAngleKey, 代碼如下:
//導(dǎo)入CIImage CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"hua.jpeg"]]; //創(chuàng)建出Filter濾鏡 CIFilter *filter = [CIFilter filterWithName:@"CIPixellate"]; [filter setValue:ciImage forKey:kCIInputImageKey]; [filter setDefaults]; CIImage *outImage = [filter valueForKey:kCIOutputImageKey]; CIFilter *filterTwo = [CIFilter filterWithName:@"CIHueAdjust"]; [filterTwo setValue:outImage forKey:kCIInputImageKey]; [filterTwo setDefaults]; [filterTwo setValue:@(1.0f) forKey:kCIInputAngleKey]; //如果不增加這行新增的濾鏡不會生效 CIImage *outputImage = [filterTwo valueForKey:kCIOutputImageKey]; //用CIContext將濾鏡中的圖片渲染出來 CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef cgImage = [context createCGImage:outputImage fromRect:[outputImage extent]]; //導(dǎo)出圖片 UIImage *showImage = [UIImage imageWithCGImage:cgImage]; CGImageRelease(cgImage); UIImageView *imageView = [[UIImageView alloc] initWithImage:showImage]; imageView.center = self.view.center; [self.view addSubview:imageView];
下面來介紹怎么用openGLES來使用濾鏡渲染圖片
使用openGlES的步驟大致如下:
#1.導(dǎo)入要渲染的圖片
#2.獲取OpenGLES渲染的上下文
#3.創(chuàng)建出渲染的GLKView buffer
#4.創(chuàng)建CoreImage的上下文
#5.進(jìn)行CoreImage的相關(guān)設(shè)置
#6.開始渲染并顯示圖片
參考代碼如下:
//導(dǎo)入要渲染的圖片 UIImage *showImage = [UIImage imageNamed:@"hua.jpeg"]; CGRect rect = CGRectMake(0, 0, showImage.size.width, showImage.size.height); //獲取OpenGLES渲染的上下文 EAGLContext *eagContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; //創(chuàng)建出渲染的buffer GLKView *glkView = [[GLKView alloc] initWithFrame:rect context:eagContext]; [glkView bindDrawable]; [self.view addSubview:glkView]; //創(chuàng)建出CoreImage的上下文 CIContext *ciContext = [CIContext contextWithEAGLContext:eagContext options:@{kCIContextWorkingColorSpace: [NSNull null]}]; //CoreImage相關(guān)設(shè)置 CIImage *ciImage = [[CIImage alloc] initWithImage:showImage]; CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"]; [filter setValue:ciImage forKey:kCIInputImageKey]; [filter setValue:@(0) forKey:kCIInputIntensityKey]; //開始渲染 [ciContext drawImage:[filter valueForKey:kCIOutputImageKey] inRect:CGRectMake(0, 0, glkView.drawableWidth, glkView.drawableHeight) fromRect:[ciImage extent]]; [glkView display];
如果要動態(tài)渲染, 可以通過UISilder動態(tài)調(diào)整一下代碼的vaule值
[filter setValue:vaule forKey:kCIInputIntensityKey];
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
詳解iOS 驗(yàn)證碼輸入的實(shí)現(xiàn)思路
這篇文章主要介紹了iOS 驗(yàn)證碼輸入一種實(shí)現(xiàn)思路,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10iOS利用UIScrollView實(shí)現(xiàn)圖片的縮放實(shí)例代碼
本篇文章主要介紹了iOS利用UIScrollView實(shí)現(xiàn)圖片的縮放實(shí)例代碼,具有一定的參考價值,有興趣的可以了解一下2017-07-07深入解析iOS應(yīng)用開發(fā)中對設(shè)計(jì)模式中的橋接模式的使用
這篇文章主要介紹了iOS應(yīng)用開發(fā)中對設(shè)計(jì)模式中的橋接模式的使用,bridge橋接模式中主張把抽象部分與實(shí)現(xiàn)部分分離,需要的朋友可以參考下2016-03-03iOS實(shí)現(xiàn)時間顯示幾分鐘前,幾小時前以及剛剛的方法示例
這篇文章主要介紹了如何利用iOS實(shí)現(xiàn)時間顯示是在幾小時前,幾分鐘前以及剛剛的格式,類似大家在qq空間和朋友圈微博看到的效果,文中給出了詳細(xì)的示例代碼,有需要的朋友們可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01iOS中滑動控制屏幕亮度和系統(tǒng)音量(附加AVAudioPlayer基本用法和Masonry簡單使用)
這篇文章主要介紹了iOS中滑動控制屏幕亮度和系統(tǒng)音量(附加AVAudioPlayer基本用法和Masonry簡單使用)的相關(guān)資料,需要的朋友可以參考下2016-12-12iOS應(yīng)用開發(fā)中使用Auto Layout來適配不同屏幕尺寸
這篇文章主要介紹了iOS應(yīng)用開發(fā)中使用Auto Layout來適配不同屏幕尺寸的方法,根據(jù)Xcode IDE下的實(shí)際調(diào)試步驟講解其用法,需要的朋友可以參考下2016-03-03