IOS – SWIFT – CollectionView使用图像,从数组加载URL

我有一个集合视图和数组,其中包含不同图像的URL。 当我启动应用程序时,集合视图开始通过数组加载图像:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CellView var url = arr[indexPath.row] var urls = NSURL(string: url) var data = NSData(contentsOfURL: urls!) cell.ImageView.image = UIImage(data: data!) return cell } 

麻烦似乎:例如在第4个细胞集合视图中加载所有4个细胞的所有4个url,并且需要很多时间。 集合视图如何加载特定单元格的特定URL,并且不花时间将URL加载到已加载的单元格?

谢谢你的帮助!!

我建议在这个问题上使用第三方库,它叫做SDWebImage 。

而且对于单元格集内的每个图像视图:

 self.imageView.sd_setImageWithURL(url, completed: block) 

或者你可以使用类似的第三方库,就像Asaf告诉你的那样。 我以前使用HANEKE进行DL /缓存图像。

看看: https : //github.com/Haneke/HanekeSwift

🙂