gpt4 book ai didi

ios - Firebase ImageView 数组

转载 作者:行者123 更新时间:2023-11-30 12:21:59 26 4
gpt4 key购买 nike

在我的应用程序中,我使用自定义图像数组 View 。用户每个帖子上传的图像数量应该是以轮播方式显示的图像数量。

在 firebase 中,我有从 image 到 imageFive 的值

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

正如您在此示例中看到的,只有一个值包含字符串。用户可以发布一到五张图像。我想附加用户上传的许多图像并将其显示在轮播中。这就是我现在所拥有的。

    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


}

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

此外,当我单击包含多个图像的单元格时,第一次单击时它什么也不显示,第二次单击时,应用程序崩溃并显示 fatal error :索引超出范围

最佳答案

我不确定,但你也可以尝试一下:

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
}

关于ios - Firebase ImageView 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44690294/

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