简体   繁体   English

网络通话后,如何将所有图像附加到此阵列?

[英]How do I append all Images to this array after Network call?

I am parsing JSON into custom objects. 我正在将JSON解析为自定义对象。 Then I am converting those url string into image and loading it to a collectionView. 然后,我将这些url字符串转换为图像并将其加载到collectionView。 The issue is I want to append all to UIImages into an array but the only images I can append come as the images show on screen/scroll. 问题是我想将所有UIImages追加到一个数组中,但是我可以追加的唯一图像是在屏幕/滚动条上显示的图像。 How can I solve this issue? 我该如何解决这个问题? Here is my code 这是我的代码

import UIKit

class HomepageCollectionViewController: UICollectionViewController {
    var hingeImagesArray = [HingeImage]()
    var arrayToHoldConvertedUrlToUIImages = [UIImage]()
    var task: NSURLSessionDataTask?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Makes the network call for HingeImages
        refreshItems()
    }

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return hingeImagesArray.count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imageReuseCell", forIndexPath: indexPath) as! ImageCollectionViewCell
        let image = hingeImagesArray[indexPath.row]

        if let imageURL = image.imageUrl {
            if let url = NSURL(string: imageURL) {
                // Request images asynchronously so the collection view does not slow down/lag
                self.task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in
                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        // Check if there is data returned
                        guard let data = data else {
                            return
                        }

                        // Create an image object from our data and assign it to cell

                        if let hingeImage = UIImage(data: data){
                             cell.collectionViewImage.image = hingeImage

                            //append converted Images to array so we can send them over to next view - only proble in that the only images converted at the ones you scrool to which is retarted

                            self.arrayToHoldConvertedUrlToUIImages.append(hingeImage)
                        }
                    })
                })

                task?.resume()
            }
        }

        return cell
    }

iOS collection and table views do not load all cells upon view load like an HTML table. iOS集合视图和表视图不会像HTML表那样在视图加载时加载所有单元格。 The line that you have 你所拥有的线

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imageReuseCell", forIndexPath: indexPath) as! ImageCollectionViewCell

to dequeue a reusable cell is doing just that. 使可重复使用的单元出队就是这样做的。 Every N cells (usually ~20-30 for a tableview) is recycled, so the UI element stays memory-efficient upon loading large sets of data. 每N个单元格(对于一个表格视图,通常为20至30个)将被回收,因此UI元素在加载大量数据时保持内存效率。

In order to load all images at once, you will have to start loading all image urls in your arrayToHoldConvertedUrlToUIImages array in the viewDidLoad callback and cache the results (AFnetworking/Alamofire have great caching components). 为了一次加载所有图像,您将必须开始在viewDidLoad回调中的arrayToHoldConvertedUrlToUIImages数组中加载所有图像url并缓存结果(AFnetworking / Alamofire具有出色的缓存组件)。 Then, load the image within the collection view cell from the cache and your scrolling UX will more streamlined. 然后,从缓存中将图像加载到集合视图单元中,滚动的UX将会更加简化。

However, I would suggest not going this caching route if you have A LOT of images because you could potentially crash your app. 但是,如果您有很多图像,建议不要使用这种缓存路线,因为这可能会导致应用崩溃。

In the gist, the cacheImages method will first check for the image if it exists in the cache (it doesn't obviously) and then download it in binary form into the cache. 根据要点,cacheImages方法将首先检查图像是否存在于缓存中(显然没有),然后以二进制形式将其下载到缓存中。 Then, after all images are downloaded and the collection view is refreshed, the cell will assign the image from the cache and since it is already in memory, it will load quickly. 然后,在下载所有图像并刷新收集视图之后,单元将从缓存中分配图像,并且由于它已在内存中,因此它将快速加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM