Firebase存储图像不会附加到数组。

huangapple go评论84阅读模式
英文:

Firebase storage image won't append to array

问题

I'm here to help with your translation request. Here's the translated part:

所以,我正在尝试下载一组图像,以在我的应用程序中在CollectionView中显示。我将这些图像存储在Firebase存储中,并将它们的URL存储在我的数据库中,每个项目节点下都有一个名为"img"的节点,具有一个名为"storage"的子节点,其中URL是键。

我首先创建了一个空的UIImage数组:var somePicArray = [UIImage](),然后通过以下函数将图片附加到这个数组中:

func test() {
    let penis = Database.database().reference().child("kategorier").child("brus")
    penis.observeSingleEvent(of: .value) { (snapshot: DataSnapshot) in
        for item in snapshot.children {
            let snap = item as! DataSnapshot
            let imageSnap = snap.childSnapshot(forPath: "img/storage")
            if let url = imageSnap.value as? String {
                let someRef = self.storageRef.reference(forURL: url)
                someRef.getData(maxSize: 10 * 10024 * 10024) { data, error in
                    if let error = error {
                        print(error)
                    } else {
                        let image = UIImage(data: data!)
                        self.testPicArray.append(image!)
                        self.collectionView?.reloadData()
                    }
                }
            }
        }
    }
}

直到"if let url"这一行,一切都正常,因为我已经尝试在此if语句内打印URL,并且它完美地打印出所有的URL作为字符串。

然而,我对第二部分有点不确定。我在我的应用程序的其他地方使用了这段代码块来显示来自Firebase存储的图片,这是有效的。然而,在这个函数中似乎不起作用,我不确定我做错了什么。

为了在单元格中显示图像,我尝试在CollectionView的cellForItemAtIndexPath方法中使用以下代码:

cell.cellimage.image = somePickArray[2]

我只是选择了数组中的随机一个数字来查看是否可以显示任何图片,但它会在这一行崩溃,显示索引超出范围的错误,我认为这是因为数组为空。

英文:

So I'm trying to download a set of images to display in a collectionview in my app. I have the images in the firebase storage, and their url in my database under the node "img" in each item node with a child "storage" with the url as the key.

I started by creating an empty array of UIImage: var somePicArray = [UIImage]() and then appending the pictures to this array through this funciton:

func test() {
    let penis = Database.database().reference().child("kategorier").child("brus")
    penis.observeSingleEvent(of: .value) { (snapshot: DataSnapshot) in
        for item in snapshot.children {
            let snap = item as! DataSnapshot
            let imageSnap = snap.childSnapshot(forPath: "img/storage")
            if let url = imageSnap.value as? String {
            let someRef = self.storageRef.reference(forURL: url)
                someRef.getData(maxSize: 10 * 10024 * 10024) { data, error in
                    if let error = error {
                        print(error)
                    } else {
                        let image = UIImage(data: data!)
                        self.testPicArray.append(image!)
                        self.collectionView?.reloadData()
                    }
                }
            }
        }
    }
}

up until the "if let url" line everything is ok, as I have tried printing the url within this if statement, and it prints the all the urls as strings perfectly.

however I'm a little more uncertain about the seccond part. I have used this block of code elsewhere in my app to display pictures from firebase storage and this works fine. However it does not seem to work within this funciton, and I'm not sure what I'm doing wrong.

In order to display the images in the cells I tried using the following code in the collectionview cellforitematindexpath method:

cell.cellimage.image = somePickArray[2] 

I just chose a random number in the array to see if i can display any picture but it just crashes saying index out of range at this line, which I assume is because the array is empty.

答案1

得分: 1

当视图控制器出现时,数组为空,因此

cell.cellimage.image = somePickArray[2]

将在数组填充数据之前崩溃,您需要确保在 numberOfItemsAtRow 中执行

return somePickArray.count
英文:

When the vc appears the array is empty so

cell.cellimage.image = somePickArray[2] 

will crash before array is filled with data , you need to make sure you do

return somePickArray.count 

inside numberOfItemsAtRow

huangapple
  • 本文由 发表于 2020年1月4日 00:05:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定