我有一个问题。我的UICollectionView的滚动太卡顿了,因为单元格中的图像大小太大。缩放函数几乎没有改变什么。而且由于图像数量,我无法制作小的副本。
如果我需要像原生相册应用程序一样快速加载图像,我该怎么办?我听说过“懒加载”,但我找不到任何关于Swift的教程。
这是我的cellForItemAtIndexPath:
let cell : MyCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! MyCollectionViewCell
let painting = objs[indexPath.row]
let name = (painting.valueForKey("imageName") as? String)!
let path = NSBundle.mainBundle().bundlePath.stringByAppendingString("/\(name)") as NSString
cell.imageView.image = UIImage(contentsOfFile: path as String)
cell.layer.shouldRasterize = true
cell.layer.rasterizationScale = UIScreen.mainScreen().scale
return cell
如果你能帮我解决这个问题,我将不胜感激! 附注:应用程序无法从网络加载。所有图片都在设备上的应用程序中。
更新
最终,我实现了SDWebImage。这是我找到的最佳解决方案。 以下是我的cellForRowAtIndexPath:
let cell : MyCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! MyCollectionViewCell
let block: SDWebImageCompletionBlock! = {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType, imageURL: NSURL!) -> Void in
// println(self)
}
let path = NSBundle.mainBundle().bundlePath.stringByAppendingString("/\(images[indexPath.row])") as NSString
let url = NSURL.fileURLWithPath(path as String)
println(url)
cell.imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "placeholder"), completed: block)
cell.layer.shouldRasterize = true
cell.layer.rasterizationScale = UIScreen.mainScreen().scale
return cell