将firebase映像放入数组中

时间:2017-10-04 19:17:30

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

我正在使用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")

               }
            }

将图像放入阵列中。但是,有时我的三张照片不会显示,有时它只显示一张照片。但是当它显示三张图片时,图片会按顺序显示。我不确定为什么有时当我点击想要有三张图片的单元格时,会出现三张图片,有时会出现两张图片,或者有时会出现一张图片。

1 个答案:

答案 0 :(得分:0)

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

 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)