Firebase imageview阵列
问题描述:
在我的应用程序中,我正在使用自定义图像数组视图。用户上传的每个帖子的许多图像应该是以旋转木马般的方式显示的许多图像。Firebase imageview阵列
在火力点,我从数值形象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
}
现在,我必须单击两次单元格才能显示图像。当我单击单元格时,一旦它不显示任何内容,则会在第二次单击时显示图像。同样,如果我点击另一个单元格,它会在第一次单击时显示前一个单元格的图像,然后在第二次单击时显示该单元格的图像。
而且当我点击多个图像的单元格,在第一次点击它说明不了什么,第二次点击,应用程序崩溃,并说致命错误:超出范围的索引
答
我不是当然,但你也可以试试这个:
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
}
我有点想象除了imageArray.removeAll()行之外的所有东西。我把它放在那里,因为当我从一个单元格移动到另一个单元格时,图像会被追加。因此,来自单元格1的图像会附加到单元格2中的图像,并将显示在旋转木马中,我不想但现在imageArray.removeAll()不起作用。 – juelizabeth
我更新了我的代码,以我到目前为止。所以我现在正在第一次点击获取图像。现在我所说的唯一问题就是细胞图像的追加 – juelizabeth