【问题标题】:Place firebase images into an array将 Firebase 图像放入数组中
【发布时间】:2018-03-16 07:12:57
【问题描述】:

我正在使用 icarousel,我想将从 Firebase 获取的图像放入一个数组并按顺序显示图像。所以我创建了一个函数。用户一次最多可以发布 3 张图片,我将这三张图片放入一个数组中。

 func bookImageArray() {
       databaseRef.child("books").child(self.postID!).observeSingleEvent(of: .value, with: { (snapshot) in


                //Check to see if a first image for the book exists
            if snapshot.hasChild("image"){
                if let stringImage = self.imageNames {


                    let imageRef = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage)")

                    imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
                        if error == nil {


                            self.ImageOne = UIImage(data: data!)!
                            imageArray.append(self.ImageOne)


                         imageArray = [self.ImageOne]


                        }else {
                            print("Error downloading image:" )
                        }

                        self.carouselView.reloadData()
                        self.carouselView.type = .linear

                    })}
                print("image exists")
            }

            //Check to see if a second image for the book exists
            if snapshot.hasChild("imageTwo") {
                if let stringImages = self.imagesTwo {

                    let imageRefs = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImages)")

                    imageRefs.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
                        if error == nil {

                            self.ImageTwo = UIImage(data: data!)!
                            imageArray.append(self.ImageTwo)


                           imageArray = [self.ImageOne,self.ImageTwo]


                        } else {
                            print("Error downloading image:" )
                        }
                        self.carouselView.reloadData()
                        self.carouselView.type = .linear

                    })}

                print("imageTwo exists")

                //Check to see if a third image for the book exists
            }

            if snapshot.hasChild("imageThree") {
                if let stringImage3 = self.imagesThree {

                    let imageRef = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage3)")

                    imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
                        if error == nil {

                            self.ImageThree = UIImage(data: data!)!
                            imageArray.append(self.ImageThree)


                            imageArray = [self.ImageOne,self.ImageTwo,self.ImageThree]

                        }else {
                            print("Error downloading image:" )
                        }

                        self.carouselView.reloadData()
                        self.carouselView.type = .linear

                    })}
                print("imageThree exists")

               }
            }

图像被放入数组中。但是,有时我的三张图片不显示,有时只显示一张图片。但是当它确实显示三张图片时,这些图片是按顺序显示的。我不知道为什么有时当我单击假定有三张图片的单元格时,会出现三张图片,有时会出现两张图片或有时会出现一张图片。

【问题讨论】:

    标签: ios arrays swift firebase-realtime-database firebase-storage


    【解决方案1】:

    无需执行以下操作,因为您已经在附加元素。

     imageArray = [self.ImageOne]
    

    请检查以下内容:

    var imageArray:[UIImage] = []
    
    func bookImageArray() {
        databaseRef.child("books").child(self.postID!).observeSingleEvent(of: .value, with: { (snapshot) in
            //Check to see if a first image for the book exists
            if snapshot.hasChild("image"){
                if let stringImage = self.imageNames {
                    let imageRef = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage)")
    
                    imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
                        if error == nil {
                            if let imageOne = UIImage(data: data!) {
                                imageArray.insert(imageOne, at: imageArray.count)
    
                            }
                        }else {
                            print("Error downloading image:" )
                        }
                        self.carouselView.reloadData()
                        self.carouselView.type = .linear
                    })}
                print("image exists")
            }
    
            //Check to see if a second image for the book exists
            if snapshot.hasChild("imageTwo") {
                if let stringImages = self.imagesTwo {
                    let imageRefs = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImages)")
                    imageRefs.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
                        if error == nil {
                            if let imageTwo = UIImage(data: data!) {
                                imageArray.insert(imageTwo, at: imageArray.count)
                            }
                        } else {
                            print("Error downloading image:" )
                        }
                        self.carouselView.reloadData()
                        self.carouselView.type = .linear
    
                    })}
    
                print("imageTwo exists")
    
                //Check to see if a third image for the book exists
            }
    
            if snapshot.hasChild("imageThree") {
                if let stringImage3 = self.imagesThree {
                    let imageRef = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage3)")
                    imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
                        if error == nil {
                            if let imageThree = UIImage(data: data!) {
                                imageArray.insert(imageThree, at: imageArray.count)
                            }
                        }else {
                            print("Error downloading image:" )
                        }
                        self.carouselView.reloadData()
                        self.carouselView.type = .linear
                    })}
                print("imageThree exists")
            }
        }
    })
    print(imageArray)
    

    【讨论】:

    • 没有imageArray = [self.ImageOne] 图像不会按顺序显示
    • 图像显示乱序,我希望它显示为 imageOne, imageTwo, imageThree
    • 我用insert代替append
    • 它崩溃了,我得到了fatal error: Array index is out of range
    • 请告诉我你是如何声明imageArray的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 2013-04-24
    • 1970-01-01
    相关资源
    最近更新 更多