如何确定什么时候所有的图像已经从Swift中的一组下载?

我正在使用PinRemoteImage库来下载填充collectionView图像。 我想基于图像高度dynamic更新单元格高度,所以我需要知道何时下载了所有图像,以便我可以重新加载/无效我的collectionViewLayout 。 什么是最好的方式来确定什么时候没有更多的图像下载?

 override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PinCollectionViewCell cell.pinImage?.pin_updateWithProgress = true cell.pinImage?.image = nil if let pinImageURL = self.pins[indexPath.row].largestImage().url { cell.pinImage?.pin_setImageFromURL(pinImageURL, completion: ({ (result : PINRemoteImageManagerResult) -> Void in if let image = result.image { self.imageArray.append(image) } 

我从来没有使用过PinRemoteImage图像库,但是当您从iCloud下载不起作用时,在完成块上回复,导致daft进程背景,然后在下载完所有数据之前触发完成子句。 这是我需要实现以解决此问题的CloudKit代码的摘录。

它在这里使用一个NSOperationalQueue,代码。 警告如果您的任何图片下载失败,此代码将进入无限循环,最好创build僵尸,最坏的情况下停止您的应用程序。

注意self.filesQ.returnQThumbs是检查下载的图像的大小的子程序,如果它们大于零,它有一个,如果他们是零/无仍然下载…它返回它在数组中find的数字。 self.filesQ.qcount()返回它正在下载的图像的数量。

 func preThumb() { newQueue = NSOperationQueue() let operation2 = NSBlockOperation(block: { print("Downloaded, you can now use") }) operation2.completionBlock = { print("Operation 2 completed") } let operation1 = NSBlockOperation(block: { var allclear = 0 while (allclear != self.filesQ.qcount()) { allclear = self.filesQ.returnQThumbs().count } }) operation1.completionBlock = { print("Operation 1 completed") // your code to display images could go here! self.newQueue.addOperation(operation2) } operation1.qualityOfService = .Background newQueue.addOperation(operation1) } 

显然,也许不是你需要运行此方法之前,开始下载理想。