如何确定何时从Swift中的一组中下载所有图像?

时间:2016-03-01 04:24:56

标签: ios swift uicollectionview

我使用PinRemoteImage库下载填充collectionView的图片。我想根据图像高度动态更新单元格高度,因此我需要知道何时下载所有图像,以便我可以重新加载/使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)

                }

1 个答案:

答案 0 :(得分:1)

我从未使用过PinRemoteImage图像库,但是当你从iCloud下载不起作用时回复完成块会导致愚蠢的过程背景,然后在下载完所有数据之前触发你的完成子句。这是我需要实现的CloudKit代码的摘录。

这里使用的是NSOperationalQueue代码。警告如果您的任何图片下载失败,此代码将进入无限循环,最多创建僵尸,最坏的情况下停止您的应用。

注意self.filesQ.returnQThumbs是一个子程序,它检查下载的图像的大小,如果它们大于零,它有一个,如果它们是零/零仍在下载...它返回它找到的数字数组。 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)
}

显然,您可能不需要在开始理想下载之前运行此方法。

相关问题