您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

CoreImage 濾鏡實作:用 GLKView 做即時可調的影像渲染

編輯:iOS 開發綜合

Core Image 是 iOS 上處理影像的強力框架,內建上百種濾鏡。要讓效果「即時」呈現在畫面上,關鍵是把 CIContext 綁定到 OpenGL ES 的 EAGLContext,再把結果畫進 GLKView。這樣每次拖動滑桿,畫面都能立即更新,適合用來做相機預覽或照片編輯器的效果預覽。

為什麼需要 GLKit

直接把濾鏡結果轉成 UIImage 再塞進 UIImageView 也可以,但每次都要重新配置與繪製,效能差。改用 GLKView 之後,渲染目標就是 GPU 的 drawable buffer,達到接近實時的更新速度。使用前需在專案中導入 GLKit.framework。

完整程式碼

// ViewController.m
#import "ViewController.h"
#import <GLKit/GLKit.h>     // 需要導入此框架

@interface ViewController ()
@property (nonatomic, strong) GLKView  *glkView;   // 渲染用的 buffer 視圖,可即時更新
@property (nonatomic, strong) CIFilter *filter;
@property (nonatomic, strong) CIImage  *ciImage;
@property (nonatomic, strong) CIContext *ciContext;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImage *showImage = [UIImage imageNamed:@"demo1.jpg"];
    CGRect rect = CGRectMake(0, 0, showImage.size.width, showImage.size.height);

    // 1. 取得 OpenGL ES 渲染上下文
    EAGLContext *eagContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    // 2. 建立渲染 buffer 視圖
    self.glkView = [[GLKView alloc] initWithFrame:rect context:eagContext];
    [self.glkView bindDrawable];        // 綁定繪製目標,否則起始畫面會是黑的
    [self.view addSubview:self.glkView];

    // 3. 建立 Core Image 上下文,並與 EAGL 上下文共用
    self.ciContext = [CIContext contextWithEAGLContext:eagContext
                                              options:@{kCIContextWorkingColorSpace : [NSNull null]}];

    // 4. 建立影像與濾鏡
    self.ciImage = [[CIImage alloc] initWithImage:showImage];
    self.filter  = [CIFilter filterWithName:@"CISepiaTone"];   // 復古褐色調
    [self.filter setValue:self.ciImage forKey:kCIInputImageKey];
    [self.filter setValue:@(0) forKey:kCIInputIntensityKey];   // 強度 0 ~ 1

    [self render];

    // 5. 加上滑桿做動態調整
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(30, 500, 200, 10)];
    slider.minimumValue = 0.f;
    slider.maximumValue = 10.f;
    [slider addTarget:self action:@selector(changeColorValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];
}

// 把渲染流程獨立出來,避免重複程式碼
- (void)render {
    [self.ciContext drawImage:[self.filter valueForKey:kCIOutputImageKey]
                       inRect:CGRectMake(0, 0, self.glkView.drawableWidth, self.glkView.drawableHeight)
                     fromRect:[self.ciImage extent]];
    [self.glkView display];
}

- (void)changeColorValue:(UISlider *)slider {
    [self.filter setValue:self.ciImage forKey:kCIInputImageKey];
    [self.filter setValue:@(slider.value) forKey:kCIInputIntensityKey];
    [self render];
}

@end

幾個實作上的關鍵點

bindDrawable 不能省

建立 GLKView 之後若不呼叫 bindDrawable,第一次顯示會是一片黑。這一步是把 drawable 真正綁到目前的 context 上。

drawImage:inRect:fromRect: 的兩個矩形別搞混

inRect 是「畫到哪裡」,用 drawable 的尺寸;fromRect 是「從哪裡取」,用 ciImage.extent。這兩者對調會導致影像被裁掉或變形。

滑桿範圍要對應濾鏡的預期值域

範例中 CISepiaTone 的 intensity 是 0~1,滑桿卻設成 0~10。實作時請依濾鏡規格設定 maximumValue,或自己做一次比例換算(例如 slider.value / 10.0),否則超過 1 之後畫面不會再有變化。

記得處理記憶體與效能

若在實機上跑,滤镜渲染會佔用可觀的 GPU 資源。建議在滑桿的 touchUpInside 才做高品質輸出,拖曳過程中使用較小的 drawableSize 以降低運算量。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved