gpt4 book ai didi

ios - 将 firebase 图像放入数组中

转载 作者:行者123 更新时间:2023-11-30 12:06:58 25 4
gpt4 key购买 nike

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

}
}

图像被放入数组中。但是,有时我的三张图片不显示,有时只显示一张图片。但是当它确实显示三张图片时,图片会按顺序显示。我不知道为什么有时当我单击应该有三张图片的单元格时,会出现三张图片,有时会出现两张图片,有时会出现一张图片。

最佳答案

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

 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)

关于ios - 将 firebase 图像放入数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46572543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com