如何确定何时从 Swift 中的集合中下载了所有图像? [英] How to determine when all images have been downloaded from a set in Swift?

查看:7
本文介绍了如何确定何时从 Swift 中的集合中下载了所有图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PinRemoteImage 库来下载填充 collectionView 的图像.我想根据图像高度动态更新单元格高度,所以我需要知道我的所有图像何时下载,以便我可以重新加载/使我的 collectionViewLayout 无效.确定何时没有更多图像可供下载的最佳方法是什么?

I'm using the PinRemoteImage library to download images that populate a collectionView. I want to update the cell height dynamically based on the image height, so I need to know when all my images are downloaded so that I can reload/invalidate my collectionViewLayout. What's the best way to determine when there are no more images left to download?

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 下载不起作用时回复完成块,导致愚蠢的进程背景,然后触发你的完成子句在它下载所有数据之前.这是我需要实现的 CloudKit 代码的摘录.

I have never used the PinRemoteImage image library, but replying on completion blocks when your downloading from iCloud doesn't work, cause the daft process backgrounds and then fires your completion clause BEFORE it has downloaded all the data. This is an extract of my CloudKit code I needed to implement to get around that.

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

It is using an NSOperationalQueue, code here. Warning this code will enter an endless loop if any of your image downloads fails, creating zombie at best, stopping your app at worst.

注意 self.filesQ.returnQThumbs 是一个检查下载图像大小的子程序,如果它们大于零,则为 1,如果它们为 0/nil,则仍在下载...它返回找到的数字数组.self.filesQ.qcount() 返回它要下载的图像数量.

Note self.filesQ.returnQThumbs is a subroutine that checks the size of the images downloaded, if they are greater than zero it has one, if they are zero/nil its still downloading...it returns the number it found in the array. self.filesQ.qcount() returns the number of images it is looking to download.

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)
}

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

Obviously, perhaps not you need to run this method BEFORE you start downloading ideally.

这篇关于如何确定何时从 Swift 中的集合中下载了所有图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆