问题
in my app, I am using a custom image array view. A number of images the user uploads per post should be a number of images displayed in a carousel-like manner.
In firebase, I have values from image to imageFive
{
"image" : "W585B.jpg",
"imageFive" : "",
"imageFour" : "",
"imageThree" : "",
"imageTwo" : "",
}
As you can see in this example only one value has a string in it. The users have the ability to post one to five images. I want to append however many images the user uploads and display them in a carousel. Here's what I have right now.
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
}
Right now, I have to click the cell twice in order for the image to show. When I click the cell once it shows nothing but then it shows the image on the second click. Similarly, if I click another cell, it shows the image of the previous cell on the first click and then on the second click, it shows the image for that cell.
Also when I click on a cell with multiple images, on the first click it shows nothing, on the second click, the app crashes and says fatal error: Index out of range
回答1:
I'm not sure but You can try this too:
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
}
来源:https://stackoverflow.com/questions/44690294/firebase-imageview-array