我怎样才能提高实施照片过滤器的速度?

How can I rise the speed of implementing photo filters?

我有一个应用程序,在 select UICollectionViewCell 上带有一些过滤器,它为我的 UIImage 实现了该过滤器。但是它如此缓慢地应用它并且冻结。如何提高在我的图像上实施图像过滤器的速度?

func applyFilter(image: UIImage) -> UIImage {
    var filter = CIFilter(name: "CIColorCube")
    filter!.setValue(colorCubeData, forKey: "inputCubeData")
    filter!.setValue(kDimension, forKey: "inputCubeDimension")

    let sourceImage = CIImage(image: image)

    filter.setValue(sourceImage, forKey: kCIInputImageKey)
    let context = CIContext(options: nil)

    let outputCGImage = context.createCGImage(filter.outputImage!, fromRect: filter.outputImage!.extent)

    let newImage = UIImage(CGImage: outputCGImage, scale: 1.0, orientation: self.imageView.image!.imageOrientation)

    return newImage
}

这是我的过滤器代码。我称之为

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("filterCell", forIndexPath: indexPath) as! FiltersCollectionViewCell

    let op1 = NSBlockOperation { () -> Void in
        NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                cell.imageView.image = self.applyFilter(image: self.croppedImage!)
        })
    }

    self.queue!.addOperation(op1);

    return cell
}

创建 CIContext 是一项昂贵的操作 - 您应该在重用它时创建一次。

您可能还想研究 GPU 渲染目标,例如 GLKView,这会加快处理速度。