【问题标题】:How can I use a photo after download without having to download it again?(In Swift)下载后如何使用照片而无需再次下载?(在 Swift 中)
【发布时间】:2019-12-09 07:54:27
【问题描述】:

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

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

我想下载照片并放在桌子上 但是我想下次显示图片,不用重新下载了

这是我的代码

    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 })
    }

【问题讨论】:

标签: swift image caching download


【解决方案1】:

我有解决您问题的方法。

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

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

【讨论】:

    猜你喜欢
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 2014-09-10
    • 2019-10-26
    • 1970-01-01
    相关资源
    最近更新 更多