我有一个UICollectionView
,其项包含一个我正在应用CIFilters的imageview
。所以生成了大约12个项目,但当我在集合视图中滚动项目时,生成新项目时会出现一些小故障。有没有办法在没有这种延迟的情况下配置UICollectionView
的项目。
目前我正在UICollectionView
的cellForItemAtIndexPath
委托方法中应用过滤器。
// filters array
let arrayOfCIFilters = ["CIBumpDistortionLinear","CIPixellate","CISepiaTone","CITwirlDistortion","CIUnsharpMask","CIVignette","CIPhotoEffectNoir","CIColorInvert","CIMotionBlur","CIColorClamp","CIToneCurve","CIColorPosterize","CICircularScreen"]
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("c1", forIndexPath: indexPath) as! filterImagesCollectionViewCell
let ciImage = CIImage(image:originalImage)
ciFilter = CIFilter(name: arrayOfCIFilters[indexPath.row])!
ciFilter.setValue(ciImage, forKey: kCIInputImageKey)
cell.imageVIew.image = UIImage(CGImage: ciContext.createCGImage(ciFilter.outputImage!, fromRect: ciFilter.outputImage!.extent))
cell.nameLabel.text = arrayOfCIFilters[indexPath.row]
return cell
}
每次查看单元格时,都会生成CIImage、CIFilter和UIImage。请记住,只要当前显示单元格,就会调用cellForItemAtIndexPath,因此当用户滚动并返回时,会再次执行此函数。
您应该在init()方法中创建一次所有图像,并将它们保存在数组中。然后使用此功能中的图像来显示它们。