現在我有一個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'}'。是? –