iOS给图片添加滤镜&使用openGLES动态渲染图片详解及实例

 更新时间:2016年10月30日 10:19:14   投稿:lqh  
这篇文章主要介绍了iOS给图片添加滤镜&使用openGLES动态渲染图片详解及实例的相关资料,需要的朋友可以参考下

iOS给图片添加滤镜&使用openGLES动态渲染图片

给图片增加滤镜有这两种方式: CoreImage / openGLES

 下面先说明如何使用CoreImage给图片添加滤镜, 主要为以下步骤:

#1.导入CIImage格式的原始图片

#2.创建CIFilter滤镜

#3.用CIContext将滤镜中的图片渲染出来

#4.导出渲染后的图片

参考代码:

//导入CIImage
  CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"hua"]];
  
  //创建出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]];
  
  //导出图片
  UIImage *showImage = [UIImage imageWithCGImage:cgImage];
  
  CGImageRelease(cgImage);
  
  UIImageView *imageView = [[UIImageView alloc] initWithImage:showImage];
  imageView.center    = self.view.center;
  [self.view addSubview:imageView];

当要设置多个滤镜的时候, 出了新创建一个CIFilter外还要额外设定kCIInputAngleKey, 代码如下:

//导入CIImage
  CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"hua.jpeg"]];
  
  //创建出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]];
  
  //导出图片
  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]}];
  
  //CoreImage相关设置
  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];

如果要动态渲染, 可以通过UISilder动态调整一下代码的vaule值

[filter setValue:vaule forKey:kCIInputIntensityKey];

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • iOS App设计模式开发中对迭代器模式的使用示例

    iOS App设计模式开发中对迭代器模式的使用示例

    这篇文章主要介绍了iOS App设计模式开发中对迭代器模式的使用示例,示例代码为传统的Objective-C,需要的朋友可以参考下
    2016-03-03
  • iOS图片拉伸技巧(iOS5.0、iOS6.0)

    iOS图片拉伸技巧(iOS5.0、iOS6.0)

    这篇文章主要为大家详细介绍了iOS图片拉伸技巧,提供了3种图片拉伸的解决方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • ios开发UITableViewCell图片加载优化详解

    ios开发UITableViewCell图片加载优化详解

    这篇文章主要为大家介绍了ios开发UITableViewCell图片加载优化的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • 如何去掉Xcode工程中某种类型的警告

    如何去掉Xcode工程中某种类型的警告

    这篇文章主要给大家介绍了关于如何去掉Xcode工程中某种类型的警告,文中通过示例代码介绍的非常详细,对大家学习或者使用Xcode具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • swift 单例的实现方法及实例

    swift 单例的实现方法及实例

    这篇文章主要介绍了swift 单例的实现方法及实例的相关资料,需要的朋友可以参考下
    2017-07-07
  • iOS 弹幕功能的实现思路图解

    iOS 弹幕功能的实现思路图解

    这篇文章主要介绍了iOS 弹幕功能的实现思路图文详解,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-07-07
  • iOS 四种回调方法总结

    iOS 四种回调方法总结

    这篇文章主要介绍了iOS 四种回调方法总结的相关资料,需要的朋友可以参考下
    2016-10-10
  • iOS中导航栏的基本使用汇总

    iOS中导航栏的基本使用汇总

    导航栏作为iOS开发的一大空控件来说,是非常的重要这篇文章主要给大家介绍了关于iOS中导航栏的基本使用的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
    2018-07-07
  • 详解iOS 用于解决循环引用的block timer

    详解iOS 用于解决循环引用的block timer

    这篇文章主要介绍了详解iOS 用于解决循环引用的block timer,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • iOS 中Swift仿微信添加提示小红点功能(无数字)

    iOS 中Swift仿微信添加提示小红点功能(无数字)

    这篇文章主要介绍了iOS 中Swift仿微信添加提示小红点功能(无数字),非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-05-05

最新评论