如何在下载后使用照片而不必再次下载?(在Swift中)

如何在下载后使用照片而不必再次下载?(在Swift中),swift,image,caching,download,Swift,Image,Caching,Download,下载后如何使用照片而无需再次下载 我从服务器下载了一张照片,它工作正常,但我想把它保存在某个地方,这样我就可以重复使用它,而无需再次下载 我想下载照片并把它们放在桌子上 但是我想下次再看这张照片,而不用再下载了 这是我的密码 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return soroush.count } func tableView(_

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

我从服务器下载了一张照片,它工作正常,但我想把它保存在某个地方,这样我就可以重复使用它,而无需再次下载

我想下载照片并把它们放在桌子上 但是我想下次再看这张照片,而不用再下载了

这是我的密码

    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()中的代码:


我有办法解决你的问题

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

  • 下次你去那里的时候。您将优先从本地加载数据。如果您签入本地数据库中的图像数据,则会将base64转换为要显示的图像。如果没有本地数据库,可能您的保存过程不成功。您必须重新下载并重新存储。

    您想过缓存它吗?检查图像id/下载url(如果存在)?可能与@arvidurs重复是的,我想捕获它。在下次不下载图片之前,请使用翠鸟框架,该框架以非常轻量级的方式处理所有问题。
            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 })
        }