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

查看:74
本文介绍了如何确定从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是一个检查大小的子程序下载的图像,如果它们大于零,则它有一个,如果它们为零/零,它仍在下载...它返回它在数组中找到的数字。 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天全站免登陆