2
我有一個應用程序在選擇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
}
你可以讓它在後臺執行,並顯示加載欄,直到它準備好了嗎? – Eric
您是否嘗試縮小圖像尺寸?我將運行Time Profiler來查找需要優化的最密集的代碼。 –