swift - 如何在下载后使用照片而无需再次下载?(Swift)

标签 swift image caching download

如何在下载后使用照片而无需再次下载

我从服务器下载了一张照片,效果很好,但我想将其保存在某个地方,这样我就可以重复使用它,而无需再次下载

我想下载照片并将其放在 table 上 但我想下次显示图片而不需要再次下载

这是我的代码

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return soroush.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let Cell =  tableView.dequeueReusableCell(withIdentifier: "frd", for: indexPath) as! MyfriendsCell
    if soroush[indexPath.row].Country == "sssssss" {
        Cell.MainView.backgroundColor = UIColor.init(displayP3Red: 0.239, green: 0.413, blue: 0.949, alpha: 1)
    }
    Cell.FriendScore.text = String(soroush[indexPath.row].Scores)
    Cell.usernshow.text = soroush[indexPath.row].UsernameShow
    let  Fulllink  = soroush[indexPath.row].Image
    Cell.AvatarImage.downloaded(from: Fulllink)
    return Cell
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

这是下载图像的扩展名:

extension UIImageView {
    func downloaded(from url: URL, contentMode mode: UIView.ContentMode = .scaleAspectFit) {  // for swift 4.2 syntax just use ===> mode: UIView.ContentMode
        contentMode = mode
        URLSession.shared.dataTask(with: url) { data, response, error in
            guard
                let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
                let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
                let data = data, error == nil,
                let image = UIImage(data: data)
                else { return }
            DispatchQueue.main.async() {
                self.image = image
            }
            }.resume()
    }
    func downloaded(from link: String, contentMode mode: UIView.ContentMode = .scaleAspectFit) {  // for swift 4.2 syntax just use ===> mode: UIView.ContentMode
        guard let url = URL(string: link) else { return }
        downloaded(from: url, contentMode: mode)
    }

}

这些是我在 ViewdidLoad() 中的代码:

        if AppDelegate.friends.isEmpty == true {
        Alamofire.request("https://mr-fast.liara.run/MRFast/Api/getFriendlist",method :.post,encoding:JSONEncoding.default,headers: headers).responseJSON { (newresponse) in
            do {

                let decoder = JSONDecoder()
                let responsegame = try decoder.decode([myfriends].self, from: newresponse.data!)
                for each in responsegame {
                    AppDelegate.friends.append(each)
                }

                let ffff = UserID(fullname: self.Fullname, country: "sssssss", scores: Int(self.Score)!, id: "T##String", usernameShow: self.Username, avatar: self.Avatarimage)
                let rrrrrr = Friend(userID: ffff)
                AppDelegate.friends[0].friends.append(rrrrrr)
                for each in AppDelegate.friends[0].friends {
                    let new = myfriendsarray(Country: each.userID.country, Scores: each.userID.scores, IdAgain: each.userID.id, UsernameShow: each.userID.usernameShow, Image: each.userID.avatar, fullname1: each.userID.fullname ?? "")
                    self.soroush.append(new)
                    DispatchQueue.main.async {
                        self.tableview.reloadData()
                    }
                }
                self.soroush.sort(by: { $0.Scores > $1.Scores })
            }catch{
                print("error")
            }
        }
    }else {
        for each in AppDelegate.friends[0].friends {
            let new = myfriendsarray(Country: each.userID.country, Scores: each.userID.scores, IdAgain: each.userID.id, UsernameShow: each.userID.usernameShow, Image: each.userID.avatar, fullname1: each.userID.fullname ?? "")
            self.soroush.append(new)
            DispatchQueue.main.async {
                self.tableview.reloadData()
            }
        }
        self.soroush.sort(by: { $0.Scores > $1.Scores })
    }

最佳答案

我有办法解决你的问题。

  1. 第一次必须下载所有图片
  2. 您可以正常显示图像。将图像转换为后 Base64 并将我们存储在本地(userdefault、realm、coredata)

下次你去那里的时候。您将优先考虑从本地加载数据。如果您检查本地数据库有图像数据,您将把 Base64 转换为图像以显示。如果没有本地数据库,您的保存过程可能会失败。您必须重新下载并重新存储。

关于swift - 如何在下载后使用照片而无需再次下载?(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57296796/

相关文章:

swift - 快速,是否有可能在 Collection View 单元格中添加 Collection View ?

ios - 没有与键解析 json 关联的值

image - Eigen 变换 : Why do high eigenvalues indicate regions of text?

php - 重复图像 x 次

python - 如何让python成功从互联网上下载大图片

swift - 具有关联类型的泛型结构实现协议(protocol)

swift - '|'运算符(operator)为我提供不同类型的结果

php - 缓存 jQuery 检索到的数据

Mathematica 中的数据缓存

java - 过时的缓存问题