我可能错过了一些东西。我正在尝试将过滤器更改为GPUImageView
。它实际上是前两次(有时只有一次),而不是停止响应更改。我找不到从GPUImageView
删除目标的方法。
代码
for x in filterOperations
{
x.filter.removeAllTargets()
}
let f = filterOperations[randomIntInRange].filter
let media = GPUImagePicture(image: self.largeImage)
media?.addTarget(f as! GPUImageInput)
f.addTarget(g_View)
media.processImage()
有什么建议吗? * 处理我图书馆的静止图片
更新
更新代码
//Global
var g_View: GPUImageView!
var media = GPUImagePicture()
override func viewDidLoad() {
super.viewDidLoad()
media = GPUImagePicture(image: largeImage)
}
func changeFilter(filterIndex : Int)
{
media.removeAllTargets()
let f = returnFilter(indexPath.row) //i.e GPUImageSepiaFilter()
media.addTarget(f as! GPUImageInput)
f.addTarget(g_View)
//second Part
f.useNextFrameForImageCapture()
let sema = dispatch_semaphore_create(0)
imageSource.processImageWithCompletionHandler({
dispatch_semaphore_signal(sema)
return
})
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
let img = f.imageFromCurrentFramebufferWithOrientation(img.imageOrientation)
if img != nil
{
//Useable - update UI
}
else
{
// Something Went wrong
}
}
答案 0 :(得分:1)
我的主要建议是,每次要更改要应用于图像的过滤器或其选项时,都不要创建新的GPUImagePicture。这是一项昂贵的操作,因为它需要通过Core Graphics和纹理上传到GPU。
此外,由于除了上述代码之外,您没有维护对GPUImagePicture的引用,因此只要您超出范围,它就会被释放。撕下渲染链,会导致黑色图像甚至崩溃。 processImage()
是一个异步操作,因此在退出上述范围时它仍然可以正常运行。
相反,为您的图像创建并维护对单个GPUImagePicture的引用,在其上交换过滤器(或更改现有过滤器的选项),并将结果定位到GPUImageView。这将更快,流失更少的内存,并且不会让你过早地解除分配。