 iOS 使用GPUImage实现滤镜效果

ylyk5972 8年前
   <p><strong>GPUImage实现滤镜效果</strong></p>    <p>GPUImage是一个开源的图像处理第三方库,提供了100多种滤镜效果来加工处理图片,功能很强大,简单的介绍一下使用方法:</p>    <p><strong>1.从github下载GPUImage,打开iOS工程</strong></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/a921f345997d932fa2ec85d654f5ca57.png"></p>    <p><strong>2.Command+B编译,libGPUImage.a右键show In finder中复制一份静态库</strong></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/96a95c1cc6adedd5089379b7986fe628.png"></p>    <p>这个时候可以验证一下.a静态库中是否包含全了真机指令集,不全的话打包会出问题,想打包起码支持armv7(armv7s兼容),arm64</p>    <pre>  <code class="language-objectivec">终端->:heavy_dollar_sign:cd到libGPUImage.a所在的目录 -> :heavy_dollar_sign:lipo –info libGPUImage.a 查看当前指令集</code></pre>    <p>如果发现不全,可通过这个位置调整后重新编译获取.a静态库</p>    <p><img src="https://simg.open-open.com/show/21c21b4586b1f89a53bab0dbf597fafa.png"></p>    <p><strong>3.这里最好是做一步真机和模拟器静态库的合并,不然只跑真机模拟器跑步起来也挺不方便.</strong></p>    <pre>  <code class="language-objectivec">lipo –create /Users/ddd/Downloads/GPUImage-master/build/Debug-iphoneos/libGPUImage.a/Users/ddd/Downloads/GPUImage-master/build/Debug-iphonesimulator/libGPUImage.a –output /Users/ddd/Desktop/trumk12-2/framework/libGPUimage.a</code></pre>    <p>(即lipo –create 真机库路径 模拟器库路径 –output 保存路径)</p>    <p>再次查看:heavy_dollar_sign:lipo –info libGPUImage.a查看如下图即可</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/923959cea1d7a3f85445804ba8be7224.png"></p>    <p><strong>4.在自己的项目中拖libGPUImage.a和所需要的类(可以全部导入)</strong></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/01ddcf2cea4a6795a2482dfbdb0c0ca2.png"></p>    <p><strong>5.开始实现代码:</strong></p>    <pre>  <code class="language-objectivec">//图片的滤镜效果  func hightLightImage(image:UIImage,type:Int) -> UIImage {        switch type {      case 1:          //中间突出 四周暗  //                        let passthroughFilter = GPUImageLookupFilter()  //                        passthroughFilter.intensity = 0.5                              let passthroughFilter = GPUImageVignetteFilter()    //                                passthroughFilter.contrast = 0.1                              //设置要渲染的区域                              passthroughFilter.forceProcessingAtSize(image.size)                                passthroughFilter.useNextFrameForImageCapture()                                //获取数据源                              let stillImageSource = GPUImagePicture(image:image)                                stillImageSource.addTarget(passthroughFilter)                                //开始渲染                              stillImageSource.processImage()                                let finallImage = passthroughFilter.imageFromCurrentFramebuffer()                                return finallImage        case 2:          //红                              let passthroughFilter = GPUImageRGBFilter()                                passthroughFilter.red = 0.9                                passthroughFilter.green = 0.8                                passthroughFilter.blue = 0.9                                //设置要渲染的区域                                passthroughFilter.forceProcessingAtSize(image.size)                                passthroughFilter.useNextFrameForImageCapture()                                //获取数据源                              let stillImageSource = GPUImagePicture(image:image)                                stillImageSource.addTarget(passthroughFilter)                                //开始渲染                              stillImageSource.processImage()                                let finallImage = passthroughFilter.imageFromCurrentFramebuffer()                                return finallImage          case 3:          //蓝                              let passthroughFilter = GPUImageRGBFilter()                                passthroughFilter.red = 0.8                                passthroughFilter.green = 0.8                                passthroughFilter.blue = 0.9                                //设置要渲染的区域                                passthroughFilter.forceProcessingAtSize(image.size)                                passthroughFilter.useNextFrameForImageCapture()                                //获取数据源                              let stillImageSource = GPUImagePicture(image:image)                                stillImageSource.addTarget(passthroughFilter)                                //开始渲染                              stillImageSource.processImage()                                let finallImage = passthroughFilter.imageFromCurrentFramebuffer()                                return finallImage          case 4:          //绿                              let passthroughFilter = GPUImageRGBFilter()                                passthroughFilter.red = 0.8                                passthroughFilter.green = 0.9                                passthroughFilter.blue = 0.8                                //设置要渲染的区域                                passthroughFilter.forceProcessingAtSize(image.size)                                passthroughFilter.useNextFrameForImageCapture()                                //获取数据源                              let stillImageSource = GPUImagePicture(image:image)                                stillImageSource.addTarget(passthroughFilter)                                //开始渲染                              stillImageSource.processImage()                                let finallImage = passthroughFilter.imageFromCurrentFramebuffer()                                return finallImage          case 5:          //怀旧                              let passthroughFilter = GPUImageSepiaFilter()                                //设置要渲染的区域                                passthroughFilter.forceProcessingAtSize(image.size)                                passthroughFilter.useNextFrameForImageCapture()                                //获取数据源                              let stillImageSource = GPUImagePicture(image:image)                                stillImageSource.addTarget(passthroughFilter)                                //开始渲染                              stillImageSource.processImage()                                let finallImage = passthroughFilter.imageFromCurrentFramebuffer()                                return finallImage        case 6:          //朦胧加暗                              let passthroughFilter = GPUImageHazeFilter()                                //设置要渲染的区域                                passthroughFilter.forceProcessingAtSize(image.size)                                passthroughFilter.useNextFrameForImageCapture()                                //获取数据源                              let stillImageSource = GPUImagePicture(image:image)                                stillImageSource.addTarget(passthroughFilter)                                //开始渲染                              stillImageSource.processImage()                                let finallImage = passthroughFilter.imageFromCurrentFramebuffer()                                return finallImage        case 7:          //饱和                              let passthroughFilter = GPUImageSaturationFilter()                                passthroughFilter.saturation = 1.5                                //设置要渲染的区域                                passthroughFilter.forceProcessingAtSize(image.size)                                passthroughFilter.useNextFrameForImageCapture()                                //获取数据源                              let stillImageSource = GPUImagePicture(image:image)                                stillImageSource.addTarget(passthroughFilter)                                //开始渲染                              stillImageSource.processImage()                                let finallImage = passthroughFilter.imageFromCurrentFramebuffer()                                return finallImage      case 8:          //亮度                              //创建一个亮度的滤镜                              let passthroughFilter = GPUImageBrightnessFilter()                                passthroughFilter.brightness = 0.2                                //设置要渲染的区域                                passthroughFilter.forceProcessingAtSize(image.size)                                passthroughFilter.useNextFrameForImageCapture()                                //获取数据源                              let stillImageSource = GPUImagePicture(image:image)                                stillImageSource.addTarget(passthroughFilter)                                //开始渲染                              stillImageSource.processImage()                                let finallImage = passthroughFilter.imageFromCurrentFramebuffer()                                return finallImage          case 9:          //曝光度                              //创建一个亮度的滤镜                              let passthroughFilter = GPUImageExposureFilter()                                passthroughFilter.exposure = 0.15                                //设置要渲染的区域                                passthroughFilter.forceProcessingAtSize(image.size)                                passthroughFilter.useNextFrameForImageCapture()                                //获取数据源                              let stillImageSource = GPUImagePicture(image:image)                                stillImageSource.addTarget(passthroughFilter)                                //开始渲染                              stillImageSource.processImage()                                let finallImage = passthroughFilter.imageFromCurrentFramebuffer()                                return finallImage        case 10:          //素描                              let passthroughFilter = GPUImageSketchFilter()                                //设置要渲染的区域                                passthroughFilter.forceProcessingAtSize(image.size)                                passthroughFilter.useNextFrameForImageCapture()                                //获取数据源                              let stillImageSource = GPUImagePicture(image:image)                                stillImageSource.addTarget(passthroughFilter)                                //开始渲染                              stillImageSource.processImage()                                let finallImage = passthroughFilter.imageFromCurrentFramebuffer()                                return finallImage      case 11:           //卡通                              let passthroughFilter = GPUImageSmoothToonFilter()                                passthroughFilter.blurRadiusInPixels = 0.5                                //设置要渲染的区域                                passthroughFilter.forceProcessingAtSize(image.size)                                passthroughFilter.useNextFrameForImageCapture()                                //获取数据源                              let stillImageSource = GPUImagePicture(image:image)                                stillImageSource.addTarget(passthroughFilter)                                //开始渲染                              stillImageSource.processImage()                                let finallImage = passthroughFilter.imageFromCurrentFramebuffer()                                return finallImage        default:          return image      }          }</code></pre>    <p>还有可能出现的一种情况是,如果不是从相册获取而是直接拍照获得时拿到的图片有可能是发生偏转90度的( 网上说法:用相机拍摄出来的照片含有EXIF信息,UIImage的imageOrientation属性指的就是EXIF中的orientation信息。如果我们忽略orientation信息,而直接对照片进行像素处理或者drawInRect等操作,得到的结果是翻转或者旋转90之后的样子。这是因为我们执行像素处理或者drawInRect等操作之后,imageOrientaion信息被删除了,imageOrientaion被重设为0,造成照片内容和imageOrientaion不匹配。所以,在对照片进行处理之前,先将照片旋转到正确的方向,并且返回的imageOrientaion为0。)</p>    <p>通过以下方法可以解决:</p>    <pre>  <code class="language-objectivec">//防图片90度  func fixOrientation(img:UIImage) -> UIImage {        if (img.imageOrientation == UIImageOrientation.Up) {          return img;      }        UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale);      let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)      img.drawInRect(rect)        let normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()!      UIGraphicsEndImageContext();      return normalizedImage;    }</code></pre>    <p>附带一张效果图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/c92c7a8f22ff9b061e8baf9bc730de11.gif"></p>    <p> </p>    <p> </p>    <p>来自:http://www.jianshu.com/p/51786bcc30bf</p>    <p> </p>