The users of my app have the ability to make a post and when they make a post they can upload more than one picture. The issue I have is viewing the pictures in my app. This is how the node looks in firebase:
},
"-Kjy7sfa7pWmlawqWTsN" : {
"image" : "CfHWd.jpg",
"imageFive" : "30fQI.jpg",
"imageFour" : "uOnVB.jpg",
"imageThree" : "eHMOY.jpg",
"imageTwo" : "jQCLu.jpg",
}
This is how I retrieve them from firebase:
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!)!
}else {
print("Error downloading image:" )
}
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!)!
}else {
print("Error downloading image:" )
}
})}
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!)!
}else {
print("Error downloading image:" )
}
})}
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!)!
}else {
print("Error downloading image:" )
}
})}
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!)!
}else {
print("Error downloading image:" )
}
})}
})}
Now my issue is displaying them in a special view called icarousel. I was successfully able to display images from my assets folder in icarousel doing this:
var imageArray : NSMutableArray = NSMutableArray()
@IBOutlet weak var carouselView: iCarousel!
override func viewDidLoad() {
super.viewDidLoad()
self.carouselView.delegate = self
self.carouselView.dataSource = self
imageArray = ["imageOne", "imageTwo", "imageThree"]
carouselView.type = .wheel
carouselView.reloadData()
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
var imageView: UIImageView!
if view == nil {
imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 130))
imageView.contentMode = .scaleAspectFit
}else{
imageView = view as! UIImageView
}
imageView.image = UIImage(named: "\(imageArray.object(at: index))")
return imageView
}
public func numberOfItems(in carousel: iCarousel) -> Int {
return imageArray.count
}
However, I attempted to retrieve all the images from firebase and place them into an array:
imageArray = [self.ImageOne, self.ImageTwo, self.ImageThree, self.ImageFour, self.ImageFive]
but it doesn't work, and I am not sure what I am doing wrong This is my attempt to set up icarousel from firebase:
var imageArray : NSMutableArray = NSMutableArray()
@IBOutlet var carouselView: iCarousel!
var ImageOne = UIImage()
var ImageTwo = UIImage()
var ImageThree = UIImage()
var ImageFour = UIImage()
var ImageFive = UIImage()
var images: NSArray! = []
override func viewDidLoad() {
super.viewDidLoad()
self.carouselView.delegate = self
self.carouselView.dataSource = self
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!)!
}else {
print("Error downloading image:" )
}
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!)!
}else {
print("Error downloading image:" )
}
})}
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!)!
}else {
print("Error downloading image:" )
}
})}
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!)!
}else {
print("Error downloading image:" )
}
})}
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!)!
}else {
print("Error downloading image:" )
}
})}
})}
imageArray = [self.ImageOne, self.ImageTwo, self.ImageThree, self.ImageFour, self.ImageFive]
carouselView.type = .rotary
carouselView.reloadData()
}
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: 230, height: 270))
imageView.backgroundColor = UIColor.lightGray
imageView.contentMode = .scaleAspectFit
imageView.clipsToBounds = true
carousel.clipsToBounds = true
}else{
imageView = view as! UIImageView
}
imageView.image = imageArray[index] as? UIImage;
print(imageArray)
return imageView
}
public func numberOfItems(in carousel: iCarousel) -> Int {
return imageArray.count
}
Use an imageArray to keep your images from firebase, and reload the carousel when everything is done.
var imageArray = [UIImage]()
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
guard let error != nil else { return }
if let data = data, let image = UIImage(data: data) {
imageArray.append(image)
if imageArray.count == self.imageNames.count { carouselView.reloadData() }
}
})
}
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: 230, height: 270))
imageView.backgroundColor = UIColor.lightGray
imageView.contentMode = .scaleAspectFit
imageView.clipsToBounds = true
carousel.clipsToBounds = true
}else{
imageView = view as! UIImageView
}
imageView.image = imageArray[index]
print(imageArray)
return imageView
}