小编Nic*_*cho的帖子

如何在 Swift 中将图像放入 NSCache?

我使用 swift 4 编写了一些代码来从 URL 加载图像,但是每次将图像添加到服务器时,都需要花费大量时间将其加载到集合视图或表视图中。我想尝试将其存储在 NScache 中,但我不明白该怎么做。谁能帮助我,我是 swift 的新手:(

@objc func loadPosts() {
    
    let url = URL(string: "http://someURL/Url.php")!

    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    
    let body = "phomepost=\(homepost)"
    
    request.httpBody = body.data(using: String.Encoding.utf8)
    
    URLSession.shared.dataTask(with: request) { data, response, error in
        DispatchQueue.main.async(execute:  {
            if error == nil {
                do{
                    let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
                    
                    self.comments.removeAll(keepingCapacity: false)
                    self.images.removeAll(keepingCapacity: false)
                    self.collectionView?.reloadData()
                    
                    guard let parseJSON = json else {
                        print("Error While Parsing")
                        return
                    }
                    
                    guard let posts …
Run Code Online (Sandbox Code Playgroud)

uiimage nscache swift swift4

2
推荐指数
1
解决办法
4431
查看次数

标签 统计

nscache ×1

swift ×1

swift4 ×1

uiimage ×1