简体   繁体   English

从Firebase存储中检索图像时出错nil

[英]Error nil in retrieving image from Firebase Storage

I have trouble retrieving a UIImage from Firebase Storage, the child path seems to be correct, though the image does not get "downloaded" to be displayed. 我无法从Firebase存储中检索UIImage,子路径似乎是正确的,尽管图像没有“下载”显示。 The part about the Firebase Database is working fine, hence retrieving data, whereas the Storage one is now. 关于Firebase数据库的部分工作正常,因此检索数据,而现在存储数据。 Code and Firebase path below 代码和Firebase路径如下

I cannot understand whether the problem is in the fact that I nested the function into the .observeSingleEvent of the Database retrieving function or not. 我无法理解问题是否在于我将函数嵌套到数据库检索函数的.observeSingleEvent中。

gs://xxxyyy-xxxyyy.appspot.com/images/QhRmIcbF7AOWjZ3nrjFd7TOekrA3/FirstImage.jpg GS://xxxyyy-xxxyyy.appspot.com/images/QhRmIcbF7AOWjZ3nrjFd7TOekrA3/FirstImage.jpg

var cells : [Cella] = []

 var imageReference: StorageReference {
        return Storage.storage().reference().child("images")
    }


 var databaseReference: DatabaseReference {
     return Database.database().reference()
 }

func getDataFromFirebase() -> [Cella]{

        let queryRef = databaseReference.queryLimited(toLast: 1)
        var appCells : [Cella] = []

        queryRef.observeSingleEvent(of: .value, with: { (snapshot) in

            for snap in snapshot.children {
                var userPhoto : UIImage?
                let userSnap = snap as! DataSnapshot
                let customerUid = userSnap.key
                let userDict = userSnap.value as! [String:AnyObject]
                let description = userDict["description"] as! String
                let title = userDict["title"] as! String

                print(title)
                print(String(customerUid))
                print(description)

                self.descriptionsArray[String(customerUid)] = description
                self.titlesArray[String(customerUid)] = title
                 //error is here BECAUSE it can't retrive the image to be dispalyed. Title and description are fine
                self.imageReference.child(String(customerUid)).child("FirstImage.jpg").getData(maxSize: 10*1024*1024, completion: { (data, error) in

                    if error != nil {
                        print("\(String(describing: error?.localizedDescription))")
                    }

                    else {userPhoto = UIImage(data: data!)}


                    })
                let newCella = Cella(image: userPhoto!, title: title, bodyMessage: description)
                appCells.append(newCella)

                }
            })

        return appCells

    }

------ UPDATE ------ As suggested I changed to using firebase Firestore and saving there the download URL as well as the other information. ------ UPDATE ------如我所知,我改为使用firebase Firestore并保存下载URL以及其他信息。 Still though, I cannot seem to get the image downloading. 尽管如此,我似乎无法下载图像。 New code below. 下面的新代码。

This is the data retrieved by document.data() : xxx.yyy@gmail.com => ["userID": QhRmIcbF7AOWjZ3nrjFd7TOekrA3, "userDescription": Route66, "userImageUrl": https://firebasestorage.googleapis.com/v0/b/shardana-61183.appspot.com/o/images%2FQhRmIcbF7AOWjZ3nrjFd7TOekrA3%2FFirstImage.jpg?alt=media&token=dea541bf-d598-414e-b4ed-a917541598d5 , "userTitle": Sample] 这是document.data()检索的数据:xxx.yyy@gmail.com => [“userID”:QhRmIcbF7AOWjZ3nrjFd7TOekrA3,“userDescription”:Route66,“userImageUrl”: https ://firebasestorage.googleapis.com/v0/ b / shardana-61183.appspot.com / o / images%2FQhRmIcbF7AOWjZ3nrjFd7TOekrA3%2FFirstImage.jpg?alt = media&token = dea541bf-d598-414e-b4ed-a917541598d5 ,“userTitle”:Sample]

firestoreUsersDatabase.getDocuments { (querySnapshot, error) in
        if let error = error {
            print("Error getting the documents: \(error)")
        } else {
            for document in querySnapshot!.documents {
                print("\(document.documentID) => \(document.data())")
                let data = document.data()
                let imageUrl = data["userImageUrl"] as! String
                let title = data["userTitle"] as! String
                let description = data["userDescription"] as! String

                let urlDownloadReference = self.imageReference.reference(forURL: imageUrl)

                urlDownloadReference.getData(maxSize: 10*2014*2014, completion: { (data, error) in

                    if error != nil {
                        print("An error occurred: \(String(describing: error?.localizedDescription))")
                    } else {

                        guard let imageDownloaded = UIImage(data: data!) else {print("Image url returned nil value ERROR"); return}

                        let newCell = Cella(image: imageDownloaded, title: title , bodyMessage: description )
                        print("NEW CELL: Image \(newCell.image)")
                        appCells.append(newCell)

                    }
                })

            }
        }
    }

yes, I think you're logic needs review. 是的,我认为你的逻辑需要审查。 You need to store on your Firestore all the users data, including all the references to needed images. 您需要在Firestore上存储所有用户数据,包括对所需图像的所有引用。 On the other hand, Firebase Storage, which is a different service within Firebase will save the images an will give you download links, but it uses a different logic than Firestore. 另一方面,Firebase存储是Firebase中的一项不同服务,它将保存图像并为您提供下载链接,但它使用的逻辑与Firestore不同。

See the following example for clarification on what I mean: 请参阅以下示例以澄清我的意思:

https://firebase.google.com/docs/storage/web/download-files https://firebase.google.com/docs/storage/web/download-files

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM