现在我有一个UICollectionView(水平滚动)单元格包含UIImage。我用这个显示UIImage过滤器,就像Instagram一样。但当我滚动查看图像在实现过滤器后的外观时,它会冻结。我怎样才能像Instagram那样让它变得平滑呢?
我的代码:

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

    NSOperationQueue.mainQueue().addOperationWithBlock {
        cell.imageView.image = self.applyFilterTo(self.image!, filter: self.filtersImages[indexPath.row])
        cell.imageView.layer.cornerRadius = 5
        cell.imageView.layer.masksToBounds = true
        cell.filterLabel.text = self.filtersLabels[indexPath.row]
    }

    return cell
}

其中applyFilter()对我的图像实现了一个过滤器。
有什么办法可以使卷轴平滑并防止冻结?

最佳答案

试试这个

let queue = NSOperationQueue()

        let op1 = NSBlockOperation { () -> Void in

            let img1 = self.applyFilterTo(self.image!, filter: self.filtersImages[indexPath.row])

            NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                cell.imageView.image = img1
            })
        }


queue.addOperation(op1);
cell.imageView.layer.cornerRadius = 5
        cell.imageView.layer.masksToBounds = true
        cell.filterLabel.text = self.filtersLabels[indexPath.row]

09-30 14:46
查看更多