English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Dettagli e esempio di applicazione di filtri di immagine e rendering dinamico di immagini utilizzando openGLES su iOS

Aggiungere filtri alle immagini in iOS e utilizzare openGLES per la rendering dinamica delle immagini

Ci sono due modi per aggiungere filtri alle immagini: CoreImage e openGLES

 Di seguito, spieghiamo come utilizzare CoreImage per aggiungere filtri alle immagini, che sono principalmente i seguenti passaggi:

#1.Importare l'immagine originale nel formato CIImage

#2.Creare il filtro CIFilter

#3.Usare CIContext per rendere l'immagine del filtro

#4.Espportare l'immagine renderizzata

Esempio di codice:

//Importare CIImage
  CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"hua"]];
  //Creare il filtro Filter
  CIFilter *filter = [CIFilter filterWithName:@"CIPixellate"];
  [filter setValue:ciImage forKey:kCIInputImageKey];
  [filter setDefaults];
  CIImage *outImage = [filter valueForKey:kCIOutputImageKey];
  //Usare CIContext per rendere l'immagine del filtro
  CIContext *context = [CIContext contextWithOptions:nil];
  CGImageRef cgImage = [context createCGImage:outImage
                    fromRect:[outImage extent]];
  //导出图片
  UIImage *showImage = [UIImage imageWithCGImage:cgImage];
  CGImageRelease(cgImage);
  UIImageView *imageView = [[UIImageView alloc] initWithImage:showImage];
  imageView.center    = self.view.center;
  [self.view addSubview:imageView];

Quando si desidera impostare più filtri, oltre a creare un nuovo CIFilter, è necessario impostare anche kCIInputAngleKey, come segue:

//Importare CIImage
  CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"hua.jpeg"]];
  //Creare il filtro 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]; //Se non si aggiunge questa riga, il nuovo filtro non funzionerà
  CIImage *outputImage = [filterTwo valueForKey:kCIOutputImageKey];
  //Usare CIContext per rendere l'immagine del filtro
  CIContext *context = [CIContext contextWithOptions:nil]; 
  CGImageRef cgImage = [context createCGImage:outputImage]
                    fromRect:[outputImage extent]];
  //导出图片
  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.导入要渲染的图片

#2.获取OpenGLES渲染的上下文

#3.创建出渲染的GLKView buffer

#4.创建CoreImage的上下文

#5.进行CoreImage的相关设置

#6.开始渲染并显示图片

参考代码如下:

//导入要渲染的图片
  UIImage *showImage = [UIImage imageNamed:@"hua.jpeg"];
  CGRect rect    = CGRectMake(0, 0, showImage.size.width, showImage.size.height);
  //获取OpenGLES渲染的上下文
  EAGLContext *eagContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  //创建出渲染的buffer
  GLKView *glkView = [[GLKView alloc] initWithFrame:rect
                       context:eagContext];
  [glkView bindDrawable];
  [self.view addSubview:glkView];
  //创建出CoreImage的上下文
  CIContext *ciContext = [CIContext contextWithEAGLContext:eagContext]
                           options:@{kCIContextWorkingColorSpace: [NSNull null]}];
  //Impostazioni relative a CoreImage
  CIImage *ciImage = [[CIImage alloc] initWithImage:showImage];
  CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"];
  [filter setValue:ciImage forKey:kCIInputImageKey];
  [filter setValue:@(0) forKey:kCIInputIntensityKey];
  //Inizio del rendering
  [ciContext drawImage:[filter valueForKey:kCIOutputImageKey]
         inRect:CGRectMake(0, 0, glkView.drawableWidth, glkView.drawableHeight)
        fromRect:[ciImage extent]];
  [glkView display];

Se si desidera rendere dinamico, è possibile regolare il valore del codice tramite UISilder

[filter setValue:vaule forKey:kCIInputIntensityKey];

Grazie per aver letto, spero che possa aiutare tutti, grazie per il supporto al nostro sito!

Ti potrebbe interessare