现在我有一个UICollectionView
(水平滚动)哪些单元格包含UIImage
。我使用这个功能来显示UIImage
过滤器,就像Instagram一样。但是当我滚动查看实现过滤器后图像的外观时 - 它会冻结。我如何才能让它流畅如Instagram?如何异步加载CollectionView细胞图像?
我的代码:
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()
实现了一个过滤器,我的形象。
任何解决方案使滚动平滑,并防止冻结?
而是在主线程上的图像应用过滤器,使用后台线程。一旦你收到过滤图像,然后获得主队列和显示图像。 – Surjeet
你可以在这里使用SDWebImage。让它从https://github.com/rs/SDWebImage –
@Surjeet所以,你建议使用:'let img = self.applyFilterTo(self.image!,filter:self.filtersImages [indexPath.row])'和后来的'NSOperationQueue.mainQueue()。addOperationWithBlock {cell.imageView.image = img'}'。是? –