【问题标题】:Swift iOS: ImageUrl in Firebase databaseSwift iOS:Firebase 数据库中的 ImageUrl
【发布时间】:2017-07-20 20:05:57
【问题描述】:

我当前在存储数据库中保存用户图像的代码是:

            if let uploadData = UIImagePNGRepresentation(self.profileImage.image!)
            {


                storage.put(uploadData, metadata: nil, completion: { (metadata, error) in

                    if error != nil {
                        print(error!)
                        return
                    }
                    print(metadata!)
                })
            }

所以我的问题是如何将图像 url 返回到我的 firebase 数据库?我有存储在 user.uid 中的用户信息,如下所示:

            let ref = FIRDatabase.database().reference(fromURL: "https://databaseURL.firebaseio.com/")

            let userReference = ref.child("Users").child(uid)

            let values: [String : Any] = ["Full name": name, "Email": email, "Username": username]


            userReference.updateChildValues(values, withCompletionBlock: { (err, ref) in

                if err != nil {

                    return
                }

                let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

                let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Homepage") as! homepageViewController

                self.present(nextViewController, animated:true, completion:nil)

                //stops loading
                self.loadingIndicator.stopAnimating()
            })

那么如何将图像 URL 存储到我为每个用户拥有的 JSON 中?

规则:

{

"rules":
 {

".read": "auth != null",


".write": "auth != null"
}

}

【问题讨论】:

标签: ios firebase swift3 firebase-realtime-database firebase-storage


【解决方案1】:
  1. 首先在您的 podfile 中添加 FirebaseUI 和 SDWebImage 并安装。
  2. 创建存储引用(参见 Firebase 文档):https://Firebase.google.com/docs/storage/ios/download-files
  3. 现在您可以直接从 Firebase 存储 gs URL 加载图像,无需存储任何 HTTP URL Firebase 会在内部为您完成所有工作。

您可以在 Firebase 数据库中保存特定图像的参考名称,以识别该特定图像。

Firebase 文档中的示例:

 // Reference to an image file in Firebase Storage
 let reference = storageRef.child("images/stars.jpg")

// UIImageView in your ViewController
let imageView: UIImageView = self.imageView

// Placeholder image
let placeholderImage = UIImage(named: "placeholder.jpg")

// Load the image using SDWebImage
imageView.sd_setImage(with: reference, placeholderImage: placeholderImage)

【讨论】:

    【解决方案2】:

    Swift 4.1 简单解决方案:-

    1. 从以下位置获取您的存储图像完整路径 URL:-
      // Create a reference from a Google Cloud Storage URI
                let gsReference = Storage.storage().reference(forURL:  "gs://<your-firebase-storage-bucket>/imageFolder/abc.jpg")
                print(gsReference.fullPath)//imageFolder/abc.jpg
                print(gsReference.bucket)//yourapp-206323.appspot.com
                print(gsReference.name)//abc.jpg
                gsReference.downloadURL(completion: { (url, error) in
                    if let _url = url{
                        print(_url)
                        //use this _url in your any lazy loading third party library  and load image in your UITableViewCell
                        //in my case is user Kingfisher third party lasy loading
                          self.imgVUser.kf.setImage(with: _url)
                    }
                })
    
    1. UITableViewCell 中的结果:- http://g.recordit.co/Ks22unDP8x.gif.

    2. 更多专业参考学习:-https://firebase.google.com/docs/storage/ios/download-files

    编码愉快!!! :)

    【讨论】:

      猜你喜欢
      • 2016-09-22
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 2017-07-18
      • 1970-01-01
      相关资源
      最近更新 更多