iOS Swift GPUImage2安装,使用,奔溃,自定义滤镜等问题
最近GPUImage2又更新了,写一个全的使用文档,并且自定义了10个常用滤镜(用于美化图片)
随时更新项目中遇到问题
在GitHub中下载源码
源码链接
GUPImage源码
目录
效果展示
安装
解压源码后看到的样式 (用到的就是红色的文件)
集成步骤
1)点开Source 找到 source-operations-LookupImages 选中文件,拖动到别处(本人选择桌面)
- 复制 framework文件夹中的 GPUImage.xcodeproj 和 Source文件 复制到你的项目中,最好不要拖动,command+C 在到你的项目中 command+V 完成后如下图 (一定要复制一份过去,不然东西还在GPUImage的源码中 虽然能运行,但是打包后没有GPUImage文件)
3)在文件中把GPUImage.xcodeproj 拖到xcode中 并完成以下步骤 (个别之处略有不同 本人使用的 xcode 12.0 beta 4)
一般来说 此时就可以使用GPUImage2 了 如果想要所有的滤镜 请继续往下看
运行报错 找不到lookup_XX.png
当使用了 某些特殊滤镜时 会报找不到 lookup_xx_xx.png 图片。 这时需要我们手动导入图片了 之前拖动的LookupImages 要开始用了
在桌面中找到刚刚拖出来的文件夹 LookupImages 拖动到你的项目中 如图1 并且要看到在Bundle Resources 中成功多了这些图片 如图2 代表完成
基本GPUImage2的安装就到此结束了
运行报错Library not loaded: @rpath/GPUImage.framework/
一定是选择 new copy files Phase 时 Destination 选错,设置为frameworks 重新编译即可
demo链接
DEMO链接
如果使用demo 请在真机上运行 不要忘了配置team
使用项目时 需在真机上跑 一定要记得设置team
常见的错误
在运行GPUImage2时 有肯定奇奇怪怪的问题出现 (欢迎大家补充)
在安装完成后 import GPUImage 提示No such module ‘GPUImage’
此问题大概率是 编译还未通过,在确保安装步骤没问题后 强行Command+B 编译一次即可。
在Release模式下 奔溃EXC_BAD_ACCESS
在高版本的 xcode中 打包出来 或者 run改为Release 会报僵尸对象错误
GPUImage2/framework/Source/Apple/PictureInput.swift 119行
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, widthToUseForTexture, heightToUseForTexture, 0, GLenum(format), GLenum(GL_UNSIGNED_BYTE), imageData)
由于高版本运行时 获取ImageData 前 没有判断 runOperationSynchronously 导致的问题
修改方法
在PictureInput.swift 文件中 从87行-131行 替换为以下代码 在此编译 即可通过
if (shouldRedrawUsingCoreGraphics) {
// For resized or incompatible image: redraw
imageData = UnsafeMutablePointer<GLubyte>.allocate(capacity:Int(widthToUseForTexture * heightToUseForTexture) * 4)
let genericRGBColorspace = CGColorSpaceCreateDeviceRGB()
let imageContext = CGContext(data:imageData, width:Int(widthToUseForTexture), height:Int(heightToUseForTexture), bitsPerComponent:8, bytesPerRow:Int(widthToUseForTexture) * 4, space:genericRGBColorspace, bitmapInfo:CGImageAlphaInfo.premultipliedFirst.rawValue | CGBitmapInfo.byteOrder32Little.rawValue)
// CGContextSetBlendMode(imageContext, kCGBlendModeCopy); // From Technical Q&A QA1708: http://developer.apple.com/library/ios/#qa/qa1708/_index.html
imageContext?.draw(image, in:CGRect(x:0.0, y:0.0, width:CGFloat(widthToUseForTexture), height:CGFloat(heightToUseForTexture)))
}
// 同步运行 代码地址 https://github.com/Allen0828/GPUImage-Swift
sharedImageProcessingContext.runOperationSynchronously {
if !shouldRedrawUsingCoreGraphics {
// Access the raw image bytes directly
dataFromImageDataProvider = image.dataProvider?.data
#if os(iOS)
imageData = UnsafeMutablePointer<GLubyte>(mutating:CFDataGetBytePtr(dataFromImageDataProvider))
#else
imageData = UnsafeMutablePointer<GLubyte>(mutating:CFDataGetBytePtr(dataFromImageDataProvider)!)
#endif
}
do {
// TODO: Alter orientation based on metadata from photo
self.imageFramebuffer = try Framebuffer(context:sharedImageProcessingContext, orientation:orientation, size:GLSize(width:widthToUseForTexture, height:heightToUseForTexture), textureOnly:true)
} catch {
fatalError("ERROR: Unable to initialize framebuffer of size (\(widthToUseForTexture), \(heightToUseForTexture)) with error: \(error)")
}
glBindTexture(GLenum(GL_TEXTURE_2D), self.imageFramebuffer.texture)
if (smoothlyScaleOutput) {
glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_LINEAR_MIPMAP_LINEAR)
}
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, widthToUseForTexture, heightToUseForTexture, 0, GLenum(format), GLenum(GL_UNSIGNED_BYTE), imageData)
if (smoothlyScaleOutput) {
glGenerateMipmap(GLenum(GL_TEXTURE_2D))
}
glBindTexture(GLenum(GL_TEXTURE_2D), 0)
}
if (shouldRedrawUsingCoreGraphics) {
imageData.deallocate()
}
}
Demo展示
demo链接
DEMO链接