GPUImage实现滤镜效果
GPUImage是一个开源的图像处理第三方库,提供了100多种滤镜效果来加工处理图片,功能很强大,简单的介绍一下使用方法:
1.从github下载GPUImage,打开iOS工程
2.Command+B编译,libGPUImage.a右键show In finder中复制一份静态库
这个时候可以验证一下.a静态库中是否包含全了真机指令集,不全的话打包会出问题,想打包起码支持armv7(armv7s兼容),arm64
终端->💲cd到libGPUImage.a所在的目录 -> 💲lipo –info libGPUImage.a 查看当前指令集
如果发现不全,可通过这个位置调整后重新编译获取.a静态库
3.这里最好是做一步真机和模拟器静态库的合并,不然只跑真机模拟器跑步起来也挺不方便.
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
(即lipo –create 真机库路径 模拟器库路径 –output 保存路径)
再次查看💲lipo –info libGPUImage.a查看如下图即可
4.在自己的项目中拖libGPUImage.a和所需要的类(可以全部导入)
5.开始实现代码:
//图片的滤镜效果
func hightLightImage(image:UIImage,type:Int) -> UIImage {
switch type {
case 1:
//中间突出 四周暗
let passthroughFilter = GPUImageVignetteFilter()
//设置要渲染的区域
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
}
}
还有可能出现的一种情况是,如果不是从相册获取而是直接拍照获得时拿到的图片有可能是发生偏转90度的( 网上说法:用相机拍摄出来的照片含有EXIF信息,UIImage的imageOrientation属性指的就是EXIF中的orientation信息。如果我们忽略orientation信息,而直接对照片进行像素处理或者drawInRect等操作,得到的结果是翻转或者旋转90之后的样子。这是因为我们执行像素处理或者drawInRect等操作之后,imageOrientaion信息被删除了,imageOrientaion被重设为0,造成照片内容和imageOrientaion不匹配。所以,在对照片进行处理之前,先将照片旋转到正确的方向,并且返回的imageOrientaion为0。)
通过以下方法可以解决:
//防图片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;
}
附带一张效果图: