2016-06-12 7 views
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 
} 
+0

バックグラウンドで実行させ、準備ができるまでローディングバーを表示することができますか? – Eric

+0

イメージを縮小してみましたか?あなたが最適化するために必要な最も集中的なコードを見つけるために、Time Profilerを実行します。 –

答えて

1

CIContextを作成することを呼び出すことは、高価な操作です - あなたは、再利用時に一度、それを作成する必要があります。

GLKViewなどのGPUレンダーターゲットを調べることで、スピードアップすることもできます。

関連する問題