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

时间:2017-06-09 09:32:44

标签: ios swift firebase firebase-realtime-database icarousel

我的应用的用户可以发帖,当他们发帖时,他们可以上传多张图片。我的问题是在我的应用程序中查看图片。这是节点在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
    }

1 个答案:

答案 0 :(得分:2)

使用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

}