简体   繁体   English

NSCache在Swift中为UITableView存储图像

[英]NSCache to store images for UITableView In Swift

I'm new in swift programming and I have searched a lot about storing images with NSCache using Swift. 我是Swift编程的新手,我搜索了很多有关使用Swift使用NSCache存储图像的信息。

What I have done so far is that I'm getting id s and imageName s with JSON and I have my data in array and I was able to display image in cells with no problem. 到目前为止,我所做的是使用JSON获取idimageName ,并将数据存储在数组中,并且能够在单元格中毫无问题地显示图像。 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, How can I store and retrieve images with cache? 因此,如何使用缓存存储和检索图像?

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. 我建议您检查此库HanekeSwift (它为UIImage,NSData,JSON,String或任何其他可以读取或写入为数据的类型提供了内存和LRU磁盘缓存),至少是了解它们如何处理缓存,您可以决定使用它还是创建自己的解决方案。

Using a very easy/simple API: 使用非常简单的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)

Using the cache 使用缓存

let cache = Shared.dataCache

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

// Eventually...

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

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

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