使用缓存中的图像创建图像数组(Swift)

时间:2016-07-26 20:21:29

标签: ios swift caching firebase-storage swift-extensions

我对iOS比较陌生,如果在使用扩展程序帮助管理下载和缓存时如何创建图像数组,我们将不胜感激。我已经和他一起四处走动了几个小时,所以认为是时候进入大脑信任了。

我创建了UIImageView的扩展,以便下载和缓存这样的图像:

let imageCache = NSCache()

extension UIImageView {

func loadImageUsingCacheWithURLString(url: NSURL) {

    self.image = nil

    // First check if there is an image in the cache
    if let cachedImage = imageCache.objectForKey(url) as? UIImage {

        self.image = cachedImage

        return
    }

    else {
    // Otherwise download image using the url location in Google Firebase
    NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) in

        if error != nil {
            print(error)
        }
        else {
            dispatch_async(dispatch_get_main_queue(), {

                // Cache to image so it doesn't need to be reloaded every time the user scrolls and table cells are re-used.
                if let downloadedImage = UIImage(data: data!) {

                    imageCache.setObject(downloadedImage, forKey: url)
                    self.image = downloadedImage

                }
            })
        }
    }).resume()
    }
}

...因此非常适合加载集合视图图像。但是,我还想捕获一组图像,以便将其传递到另一个视图控制器,而不需要再次下载所有内容(浪费数据)。我创建了另一个函数来执行此操作,但imageholder.image始终为nil。我不认为这是一个异步问题,因为此时所有图像都被缓存,当我调试时,我永远不能将它变为零。

var imageHolder: UIImageView!
var imagesArray:[UIImage] = [UIImage]()

// Function to create images array to use on the photo view.
func createImagesArray() {

    for url in imagesURLArray {

        imageHolder.loadImageUsingCacheWithURLString(url)

        if imageHolder.image != nil {

        imagesArray.append(imageHolder.image!)
        }

    }
}

感觉我错过了一些简单的东西,但我被卡住了。谢谢!

2 个答案:

答案 0 :(得分:1)

我最终为返回UIImage的扩展创建了另一个函数。在这之后,没有问题附加到imagesArray。

let imageCache = NSCache()
var returnImage:UIImage = UIImage()

func returnImageUsingCacheWithURLString(url: NSURL) -> (UIImage) {

    // First check if there is an image in the cache
    if let cachedImage = imageCache.objectForKey(url) as? UIImage {

        return cachedImage
    }

    else {
        // Otherwise download image using the url location in Google Firebase
        NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) in

            if error != nil {
                print(error)
            }
            else {
                dispatch_async(dispatch_get_main_queue(), {

                    // Cache to image so it doesn't need to be reloaded everytime the user scrolls and table cells are re-used.
                    if let downloadedImage = UIImage(data: data!) {

                        imageCache.setObject(downloadedImage, forKey: url)
                        returnImage = downloadedImage

                    }
                })
            }
        }).resume()
        return returnImage
    }
}

答案 1 :(得分:0)

let imageCache = NSCache()
let images:[UIImages] = []

extension UIImageView {

func loadImageUsingCacheWithURLString(url: NSURL) {

    self.image = nil

    // First check if there is an image in the cache
    if let cachedImage = imageCache.objectForKey(url) as? UIImage {

        self.image = cachedImage

        return
    }

    else {
    // Otherwise download image using the url location in Google Firebase
    NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) in

        if error != nil {
            print(error)
        }
        else {
            dispatch_async(dispatch_get_main_queue(), {

                // Cache to image so it doesn't need to be reloaded every time the user scrolls and table cells are re-used.
                if let downloadedImage = UIImage(data: data!) {

                    imageCache.setObject(downloadedImage, forKey: url)
                    self.image = downloadedImage

                }
            })
        }
        }).resume()
    }
    //Creat array and append the last image to it. 
    self.images.append(self.image)
}