用户将图像上传到Firebase存储,并将url字符串添加到firebase数据库,
如何在Firebase数据库中获取字符串并将其转换为uiimage以将其用作图像?

从数据库返回的字符串是这样的:


  https://firebasestorage.googleapis.com/v0/b/ichatmessage.appspot.com/o/YNoodTnOSGXl6HkPoqhOPAopb8v1?alt=media&token=738771a5-53e3-46c2-b508-ad5cfa03e74e


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! NewMessageCell
    let user = users[indexPath.row]
    cell.textLabel?.text = user.username
    cell.detailTextLabel?.text = user.email
    let currentID = Auth.auth().currentUser?.uid
    let databaseRef = Database.database().reference().child("users").child(currentID!)
    databaseRef.observe(.value) { (data) in
        let dictinoray = data.value as? [String: AnyObject]
        let profileImage = dictinoray!["profileimage"] as! String
        cell.imageView?.image = profileImage.toImage()
        print(profileImage)
    }
    return cell
}


延伸

  extension String {
func toImage() -> UIImage? {
    if let data = Data(base64Encoded: self, options: .ignoreUnknownCharacters){
        return UIImage(data: data)
    }
    return nil
}
}

最佳答案

我通过映射和使用存储downloadurl来使用其他方式,它有效

          override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! NewMessageCell
    let user = users[indexPath.row]
    cell.textLabel?.text = user.username
    cell.detailTextLabel?.text = user.email
        let storageref = Storage.storage().reference().child(user.userid)
    storageref.downloadURL { (imgurl, er) in
        let imagedata = try? Data.init(contentsOf: imgurl!)
        cell.imageView?.layer.cornerRadius = (cell.imageView?.frame.size.width)!/2
        cell.imageView?.image = UIImage.init(data: imagedata!)
    }

10-05 20:23
查看更多