NSCache 在 Swift 中为 UITableView 存储图像 [英] NSCache to store images for UITableView In Swift

查看:24
本文介绍了NSCache 在 Swift 中为 UITableView 存储图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Swift 编程的新手,我搜索了很多关于使用 Swift 使用 NSCache 存储图像的信息.

I'm new in swift programming and I have searched a lot about storing images with NSCache using Swift.

到目前为止,我所做的是使用 JSON 获取 ids 和 imageNames,并且我将数据放在数组中,并且能够显示图像在细胞中没有问题.现在我想缓存图像.这是我写的代码:

What I have done so far is that I'm getting ids and imageNames with JSON and I have my data in array and I was able to display image in cells with no problem. Now I want to cache images. This is the code that I have written:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as TableViewCell

        //cell.imageView
         nameID = self.hoge[indexPath.row]["cars"]["carid"].string!
        cell.nameLabel.text = nameID

        if let imageFileURL = imageCache.objectForKey(self.hoge[indexPath.row]["cars"]["carid"].intValue) as? NSURL {
            println("Get image from cache")

        } else {

            imageName = self.hoge[indexPath.row]["cars"]["pic_name"].string!

            // If the image does not exist, we need to download it
            var imgURL: NSURL = NSURL(string: "http://192.168.1.35/car/uploads/" + imageName )!

            var image:UIImage = UIImage(named: "pen")!
            // Download an NSData representation of the image at the URL
            let request: NSURLRequest = NSURLRequest(URL: imgURL)
            NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
                if error == nil {
                    image = UIImage(data: data)!
                    cell.viewCell.image = image

                }
                else {
                    println("Error: \(error.localizedDescription)")
                }
            })

        }

        return cell
    }

SO,如何使用缓存存储和检索图像?

SO, How can I store and retrieve images with cache?

推荐答案

我建议你查看这个库 HanekeSwift(它提供UIImage、NSData、JSON、String 或任何其他可以作为数据读取或写入的类型的内存和 LRU 磁盘缓存),至少要了解它们是如何处理缓存的,您可以决定使用它还是创建自己的解决方案.

I recommend you to check this library HanekeSwift (It provides a memory and LRU disk cache for UIImage, NSData, JSON, String or any other type that can be read or written as data), at least to understand how they are dealing with cache, and you can decide to use it or create your own solution.

使用非常简单/简单的 API:

Using a very easy/simple API:

// Setting a remote image
imageView.hnk_setImageFromURL(url)

// Setting an image manually. Requires you to provide a key.
imageView.hnk_setImage(image, key: key)

使用缓存

let cache = Shared.dataCache

cache.set(value: data, key: "image.png")

// Eventually...

cache.fetch(key: "image.png").onSuccess { data in
     // Do something with data
}

这篇关于NSCache 在 Swift 中为 UITableView 存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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