【问题标题】:issue "unexpectedly found nil" with downloading image from firebase swift从 firebase swift 下载图像时发出“意外发现 nil”
【发布时间】:2017-05-14 11:04:04
【问题描述】:

我从 firebase 下载图像时遇到问题,这是我的代码:

func getuser(){

FIRDatabase.database().reference().child("users").child(userID!).child("credentials").observeSingleEvent(of: .value, with: { (snapshot) in

        let value = snapshot.value as? NSDictionary
        let name = value?["name"] as! String
        let email = value?["email"] as! String
        let profilePicLink = value?["profilePicLink"] as? String ?? ""


        Variables.userName = name as String
        Variables.userEmail = email as String
        self.username.text = Variables.userName
        print(profilePicLink)
        // Create a storage reference from the URL
        let storageRef = self.storage.reference(forURL: profilePicLink)
        // Download the data, assuming a max size of 1MB (you can change this as necessary)
        storageRef.data(withMaxSize: 1 *  1000 * 1000 ) { (data, error) -> Void in

            let pic = UIImage(data: data!)
            self.img.image = pic

        }



    })


}

我收到此错误:

==> this the link as printed https://firebasestorage.googleapis.com/v0/b/eswitch-72b56.appspot.com/o/usersProfilePics%2FKYe6fIQReNM8Oog4ELOdRLsC99J3?alt=media&token=6f2392bc-d35b-4ebc-b2c1-2dc34bc4b95a
fatal error: unexpectedly found nil while unwrapping an Optional value

我得到的错误在这一行

let storageRef = self.storage.reference(forURL: profilePicLink)

以下是我的快照读数:

snap (credentials) {
    email = "bilal@me.com";
    mobile = 50955514;
    name = Bilal;
    profilePicLink = "https://firebasestorage.googleapis.com/v0/b/eswitch-72b56.appspot.com/o/usersProfilePics%2FKYe6fIQReNM8Oog4ELOdRLsC99J3?alt=media&token=6f2392bc-d35b-4ebc-b2c1-2dc34bc4b95a";
    role = user;
}

谢谢

解决办法如下:

在 func viewDidLoad() 中添加了这一行

storage = FIRStorage.storage()

【问题讨论】:

  • 你确定self.storage已经初始化了吗?
  • 如果 profilePicLink 变量对于 self.storage.reference 来说是可选的还是不是可选的,为什么这很重要?也许 self.storage 没有定义或者问题出在其他地方。
  • 哇,我怎么错过了!谢谢!我将 storage = FIRStorage.storage() 添加到 func viewDidLoad()
  • @BilalBaydoun 请添加解决方案作为此问题的答案以供将来参考。
  • 你能发表你的评论作为答案吗谢谢!

标签: ios swift firebase firebase-storage


【解决方案1】:

您需要确保 self.storage 已初始化。

storage = FIRStorage.storage()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2021-09-06
    • 2021-09-09
    • 2020-03-28
    • 1970-01-01
    相关资源
    最近更新 更多