1

我收到了一个奇怪的问题,我无法从Firebase数据库检索个人资料照片。我没有收到任何错误,&出现图片的ImageView,但其中没有图像。我哪里错了?正在从Firebase问题中检索个人资料照片 - Swift

这是我在我的viewWillAppear中的代码:

/** Profile Picture **/ 
     profilePicture.frame = CGRect(x: self.view.frame.width/2.975, y: self.view.frame.height/3.925, width: self.view.frame.width/3 , height: self.view.frame.height/5) 
     profilePicture.layer.borderColor = UIColor.black.cgColor 
     profilePicture.layer.borderWidth = 3 
     // profilePicture.backgroundColor = UIColor.purple 
     profilePicture.layer.cornerRadius = profilePicture.layer.frame.width/2 




    let ref = FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid) 

    ref.child("pictureforprofile").observe(.value, with: {(snap: FIRDataSnapshot) in 


     let imageUrl = snap.value as! String 
     print(imageUrl) 


     self.profilePicture.sd_setImage(with: URL(fileURLWithPath: imageUrl)) 

     self.view.addSubview(self.profilePicture) 
     self.reloadInputViews() 




    }) 

当我打印出来的IMAGEURL,它出来的:

https://firebasestorage.googleapis.com/v0/b/wavelength-official.appspot.com/o/profilePicture%2F4348A9F6-A49B-4FF4-BC14-83081684E8FA.jpg?alt=media&token=a842bda6-59b0-42dc-bf24-8dbb839e7231 

这是我的数据库看起来像火力地堡 enter image description here

---所以我已经解决了这个问题,感谢下面的答案!这里是下面的代码,将来任何人都有这个问题!

/** Profile Picture **/ 
    profilePicture.frame = CGRect(x: self.view.frame.width/2.975, y: self.view.frame.height/3.925, width: self.view.frame.width/3 , height: self.view.frame.height/5) 
    profilePicture.layer.borderColor = UIColor.black.cgColor 
    profilePicture.layer.borderWidth = 3 
    // profilePicture.backgroundColor = UIColor.purple 
    profilePicture.layer.cornerRadius = profilePicture.layer.frame.width/2 

    let ref = FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid) 

    ref.child("pictureforprofile").observe(.value, with: {(snap: FIRDataSnapshot) in 

     let imageUrl = snap.value 

     let storage = FIRStorage.storage() 
     _ = storage.reference() 
     let ref = storage.reference(forURL: imageUrl as! String) 
     ref.data(withMaxSize: 1 * 1024 * 1024) { data, error in 
      if error != nil { 
       // Uh-oh, an error occurred! 
      } else { 

       self.profilePicture.image = UIImage(data: data!) 
       self.view.addSubview(self.profilePicture) 
       self.reloadInputViews() 
      } 
     } 


     // self.profilePicture.sd_setImage(with: URL(fileURLWithPath: imageUrl as! String)) 


    }) 

回答

1

对于Firebase,您无法使用您的Firebase存储的网址。如果您决定将图像存储到IMGUR或FTP服务器上,您提出的解决方案将可以正常工作。但是,您必须使用从Firebase检索到的网址并使用其Storage API下载它。

// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes) 
let storage = Storage.storage() 
let storageRef = storage.reference() 
let ref = storage.reference(forURL: imageURL) 
ref.getData(maxSize: 1 * 1024 * 1024) { data, error in 
    if let error = error { 
    // Uh-oh, an error occurred! 
    } else { 

    self.profilePicture = UIImage(data: data!) 
    self.view.addSubview(self.profilePicture) 
    self.reloadInputViews() 
    } 
} 
+0

非常感谢!有没有一种方法可以为其提供默认图像?或者我只是加载一个图像到视图中,直到下载完成? – AndrewS

+0

没问题!要提供默认图像,请将图像拖放到.xcassets中。在下载之前,请尝试'self.picture = someImage'并将其添加到您的视图中。 – Fallah

相关问题