【发布时间】: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