-1

I have appended an array with URL's like so

[FirebaseTest.storiesContent(storyUrl: https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/Content%2F20170525130622.jpg?alt=media&token=1e654c60-2f47-43c3-9298-b0282d27f66c), FirebaseTest.storiesContent(storyUrl: https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/20170525131400.mp4?alt=media&token=30fd962d-c305-4fa4-955d-dbb06ef91623), FirebaseTest.storiesContent(storyUrl: nil)]

In order to create this array, I am using a structure as a basis in order to append the content

struct storiesContent {

var storyUrl : String!

}

However, I am unsure how I would grab these URL's from this array in order to repeatedly download each image with SDWebImage in order to append each image to an array of UIImage's. I'm very new to Swift so my understanding is limited.

  • 1
    Why did you make `storyURL` implicitly unwrapped? Get rid of that `!`. – rmaddy Jun 01 '17 at 16:38
  • When I removed the ! I began receiving errors when I was appending to the array. I append the AnyObject's grabbed from my database using as! String? but now, due to the nil, the app crashes when testing. Any workarounds? I'm sorry, I don't entirely understand the issues with implicitly unwrapping – Aidan McVay Jun 01 '17 at 16:46
  • 2
    See https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu and spend time reading about optionals in the "The Swift Programming Language" book from Apple. – rmaddy Jun 01 '17 at 16:48

2 Answers2

0

I really haven't used Firebase, but for what I understand here, you want to download the image from each link and save all the images in an array. You can achieve that by doing this:

//Get all URLS in an NSArray
let urlsArray:NSArray = ["https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/Content%2F20170525130622.jpg?alt=media&token=1e654c60-2f47-43c3-9298-b0282d27f66c","https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/20170525131400.mp4?alt=media&token=30fd962d-c305-4fa4-955d-dbb06ef91623"]

//Create a NSMutableArray where the final images will be saved.
let imagesArray:NSMutableArray! = NSMutableArray()

//Create a for that checks every link in the urlsArray.
for x in 0..<urlsArray.count
{
   //Set the urlsArray content at position x as a URL
   let imageUrl:URL = URL(string: urlsArray.object(at: x) as! String)!

   //Generate a request with the current imageUrl.
   let request:URLRequest = URLRequest.init(url: imageUrl)

   //Start a NSURLConnection and get a Data that represents your image.
   NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main, completionHandler: { (response, imageDta, error) in

       //Store the received data as an UIImage.
       let imageReceived:UIImage = UIImage(data: imageDta!)!

       //Save the image to our imagesArray.
       imagesArray.add(imageReceived)
   })
   //The Process loops until you get all the images.
}

UPDATE

Sure you can, the only thing here is that I removed your last object from your array because it contains a nil object and Swift can't candle nil objects:

//Using the NSArray style you're using.
let yourFireBaseArray = [FirebaseTest.storiesContent(storyUrl: https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/Content%2F20170525130622.jpg?alt=media&token=1e654c60-2f47-43c3-9298-b0282d27f66c), FirebaseTest.storiesContent(storyUrl: https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/20170525131400.mp4?alt=media&token=30fd962d-c305-4fa4-955d-dbb06ef91623)]

//Create a NSMutableArray where the final images will be saved.
let imagesArray:NSMutableArray! = NSMutableArray()

//Create a for that checks every link in the yourFireBaseArray.
for x in 0..<yourFireBaseArray.count
{
   //Get your current array position string as a storiesContent object
   let fireBaseString:storiesContent = yourFireBaseArray.object(at: x) as! storiesContent 

   //Use your fireBaseString object, get the storyURL string and set it in an URL.
   let imageUrl:URL = URL(string: fireBaseString.storyURL)!

   //Generate a request with the current imageUrl.
   let request:URLRequest = URLRequest.init(url: imageUrl)

   //Start a NSURLConnection and get a Data that represents your image.
   NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main, completionHandler: { (response, imageDta, error) in

       //Store the received data as an UIImage.
       let imageReceived:UIImage = UIImage(data: imageDta!)!

       //Save the image to our imagesArray.
       imagesArray.add(imageReceived)
   })
   //The Process loops until you get all the images.
}

UPDATE 2

Okay, this is my example project, copy and paste and it will give you the resulted image.

import UIKit

class ViewController: UIViewController {

@IBOutlet var image:UIImageView!
var urlArray:NSMutableArray! = NSMutableArray()
var imagesArray:NSMutableArray! = NSMutableArray()

override func viewDidLoad() {
    super.viewDidLoad()
    urlArray = NSMutableArray.init(array: ["https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/Content%2F20170525130622.jpg?alt=media&token=1e654c60-2f47-43c3-9298-b0282d27f66c"])
    // Do any additional setup after loading the view, typically from a nib.
}

override func viewDidAppear(_ animated: Bool) {
    for x in 0..<urlArray.count
    {
        let imageUrl:URL = URL(string: "\(urlArray.object(at: x) as! String)")!
        let request:URLRequest = URLRequest.init(url: imageUrl)
        NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main, completionHandler: { (response, imageDta, error) in
            if (error == nil)
            {
                self.imagesArray.add(UIImage(data: imageDta!)!)
                if self.imagesArray.count > 0
                {
                    self.image.image = self.imagesArray.object(at: 0) as! UIImage
                }
            }else{
                print("ERROR - \(error!)")
            }
        })
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

enter image description here

Charlie Pico
  • 2,036
  • 2
  • 12
  • 18
0

To retrive Data from Firebase add propertylist file to your project it will hold the reference to your data base in firbase.

//Declare an array of string 
var myArray = [String]()

//call this when your view load or in viewDidLoad()
ref = FIRDatabase.database().reference()

ref?.child("path").observe(.value, with: { (snapshot) in
        if let snap = snapshot.value as? [String]{
        self.myArray = snap
        }
    })

Then menupulate your array as you like.

Subrat Padhi
  • 128
  • 11
  • That's not what I need help with, I understand how to pull data from my firebase and place it in an array, I'm having issues then grabbing the links from this array and downloading them – Aidan McVay Jun 02 '17 at 11:40