我有一个应用程序,在选择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
}