Firebase imageview数组

在我的应用程序中,我正在使用自定义图像数组视图。 用户上传的每个post的许多图像应该是以旋转木马般的方式显示的多个图像。

在firebase,我有价值从图像到imageFive

{ "image" : "W585B.jpg", "imageFive" : "", "imageFour" : "", "imageThree" : "", "imageTwo" : "", } 

正如你在这个例子中看到的,只有一个值有一个string。 用户可以发布一到五个图像。 我想追加用户上传的许多图像并将其显示在传送带上。 这是我现在所拥有的。

  var imageArray = [UIImage]() override func viewDidLoad() { super.viewDidLoad() 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!)! imageArray.append(self.ImageOne) }else { print("Error downloading image:" ) } self.carouselView.reloadData() self.carouselView.type = .rotary 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) }else { print("Error downloading image:" ) } self.carouselView.reloadData() self.carouselView.type = .rotary })} 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) }else { print("Error downloading image:" ) } self.carouselView.reloadData() self.carouselView.type = .rotary })} 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!)! imageArray.append(self.ImageFour) }else { print("Error downloading image:" ) } self.carouselView.reloadData() self.carouselView.type = .rotary })} 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!)! imageArray.append(self.ImageFive) }else { print("Error downloading image:" ) } self.carouselView.reloadData() self.carouselView.type = .rotary })} }) } 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: 250, height: 270)) // imageView.backgroundColor = UIColor.lightGray imageView.contentMode = .scaleAspectFit imageView.clipsToBounds = true carousel.clipsToBounds = true }else{ imageView = view as! UIImageView } imageView.image = imageArray[index] imageArray.removeAll() return imageView } 

现在,我必须点击两次单元才能显示图像。 当我点击单元格时,它什么都不显示,但是在第二次单击时显示图像。 同样,如果我单击另一个单元格,它会在第一次单击时显示前一个单元格的图像,然后在第二次单击时显示该单元格的图像。

另外,当我点击一个单元格与多个图像,在第一次单击它不显示任何内容,在第二次点击,应用程序崩溃,并说致命的错误:索引超出范围

我不确定,但你也可以试试这个:

 func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView { var imageView = view as! UIImageView if view != nil { imageView = UIImageView(frame: CGRect(x: 20, y: 0, width: 250, height: 270)) // imageView.backgroundColor = UIColor.lightGray imageView.contentMode = .scaleAspectFit imageView.clipsToBounds = true carousel.clipsToBounds = true imageView = view as! UIImageView imageView.image = imageArray[index] imageArray.removeAll() // are you sure about this line? } return imageView }