在自定义视图中显示 firebase 图像数组 (icarousel)

display firebase images array in custom view (icarousel)

我的应用程序的用户可以制作 post,当他们制作 post 时,他们可以上传多张图片。我遇到的问题是在我的应用程序中查看图片。这是节点在 firebase 中的样子:

  },    
  "-Kjy7sfa7pWmlawqWTsN" : {    
    "image" : "CfHWd.jpg",  
    "imageFive" : "30fQI.jpg",  
    "imageFour" : "uOnVB.jpg",  
    "imageThree" : "eHMOY.jpg", 
    "imageTwo" : "jQCLu.jpg",   
  } 

这是我从 firebase 检索它们的方式:

if let stringImage = self.imageNames {

            let imageRef = 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!)!



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

                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!)!


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



                    })}
                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!)!


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



                    })}
                if let stringImage4 = self.imagesFour {

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

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

                            self.ImageFour = UIImage(data: data!)!


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



                    })}
                if let stringImage5 = self.imagesFive {
                    AppDelegate.instance().showActivityIndicator()
                    let imageRef = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage5)")

                    imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
                        if error == nil {
                            AppDelegate.instance().dismissActivityIndicator()
                            self.ImageFive = UIImage(data: data!)!


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



                    })}



            })}

现在我的问题是在一个名为 icarousel 的特殊视图中显示它们。我成功地能够在 icarousel 中显示我的资产文件夹中的图像:

  var imageArray : NSMutableArray = NSMutableArray()
    @IBOutlet weak var carouselView: iCarousel!
override func viewDidLoad() {
        super.viewDidLoad()
self.carouselView.delegate = self
        self.carouselView.dataSource = self

imageArray = ["imageOne", "imageTwo", "imageThree"]
carouselView.type = .wheel
        carouselView.reloadData()
}

func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
        var imageView: UIImageView!

        if view == nil {
            imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 130))
            imageView.contentMode = .scaleAspectFit
        }else{
            imageView = view as! UIImageView

        }
        imageView.image = UIImage(named: "\(imageArray.object(at: index))")
        return imageView

        }


    public func numberOfItems(in carousel: iCarousel) -> Int {
        return imageArray.count
    }

但是,我试图从 firebase 中检索所有图像并将它们放入一个数组中:

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

但它不起作用,我不确定我做错了什么 这是我尝试从 firebase 设置 icarousel:

var imageArray : NSMutableArray = NSMutableArray()
@IBOutlet var carouselView: iCarousel!

var ImageOne = UIImage()
var ImageTwo = UIImage()
var ImageThree = UIImage()
var ImageFour = UIImage()
var ImageFive = UIImage()

    var images: NSArray! = []
override func viewDidLoad() {
        super.viewDidLoad()
self.carouselView.delegate = self
        self.carouselView.dataSource = self
 if let stringImage = self.imageNames {

            let imageRef = 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!)!



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

                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!)!


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



                    })}
                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!)!


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



                    })}
                if let stringImage4 = self.imagesFour {

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

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

                            self.ImageFour = UIImage(data: data!)!


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



                    })}
                if let stringImage5 = self.imagesFive {
                    AppDelegate.instance().showActivityIndicator()
                    let imageRef = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage5)")

                    imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
                        if error == nil {
                            AppDelegate.instance().dismissActivityIndicator()
                            self.ImageFive = UIImage(data: data!)!


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



                    })}



            })}


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


        carouselView.type = .rotary
        carouselView.reloadData()


    }
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
        var imageView: UIImageView!

        if view == nil {
            imageView = UIImageView(frame: CGRect(x: 20, y: 0, width: 230, height: 270))
            imageView.backgroundColor = UIColor.lightGray
            imageView.contentMode = .scaleAspectFit
            imageView.clipsToBounds = true
            carousel.clipsToBounds = true
        }else{
            imageView = view as! UIImageView

        }


        imageView.image = imageArray[index] as? UIImage;
        print(imageArray)
        return imageView

    }



    public func numberOfItems(in carousel: iCarousel) -> Int {
        return imageArray.count
    }

使用 imageArray 使您的图像远离 firebase,并在一切完成后重新加载轮播。

var imageArray = [UIImage]() 

if let stringImage = self.imageNames {
    let imageRef = storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage)")
    imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
        guard let error != nil else { return }

        if let data = data, let image = UIImage(data: data) {
            imageArray.append(image)

            if imageArray.count ==  self.imageNames.count { carouselView.reloadData() }
        }
    })
}

func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
    var imageView: UIImageView!

    if view == nil {
        imageView = UIImageView(frame: CGRect(x: 20, y: 0, width: 230, height: 270))
        imageView.backgroundColor = UIColor.lightGray
        imageView.contentMode = .scaleAspectFit
        imageView.clipsToBounds = true
        carousel.clipsToBounds = true
    }else{
        imageView = view as! UIImageView
    }


    imageView.image = imageArray[index]
    print(imageArray)
    return imageView

}