Swift iOS:Firebase数据库中的ImageUrl

时间:2017-03-01 13:10:35

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

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

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


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

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

所以我的问题是如何将图片网址恢复到我的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"
}

}

2 个答案:

答案 0 :(得分:2)

  1. 首先在podfile中添加FirebaseUI和SDWebImage并安装。
  2. 创建存储参考(请参阅Firebase文档):https://Firebase.google.com/docs/storage/ios/download-files
  3. 现在,您可以直接从Firebase Storage gs URL加载图片,无需存储任何HTTP URL Firebase将在内部为您完成所有操作。
  4. 您可以在Firebase数据库中保存特定图像的参考名称,以识别该特定图像。

    来自Firebase doc的示例:

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

答案 1 :(得分:0)

Swift 4.1简单解决方案:-

  
      
  1. 从:-
  2. 获取存储的图像的完整路径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.   
  3. 更多专业参考学习:-https://firebase.google.com/docs/storage/ios/download-files

  4.   

快乐编码!!! :)