0

I'm trying to retrieve user images from the firebase dashboard to table view cells.

I have stored the images in the storage and their url in the dashboard as this:

"UserDevices" : {
"-KRec2mZ5l9Q5QSwiWvS" : {
  "Category" : "أخرى",
  "Description" : "mmm",
  "DeviceName" : "mmm",
  "ImageUrl" : "https://firebasestorage.googleapis.com/v0/b/jabeerah-c27c3.appspot.com/o/Devices_Images%2FCF13F154-2B5F-4926-A979-2E187BD0E504.png?alt=media&token=bf1b876f-9f9a-4e78-9e8d-98d017fd0404",
  "city" : "Jeddah",
  "email" : "testing94@gmal.com",
  "name" : "Mariah",
  "phone" : "00000"
   }
 }

Here's code:

class CategoryDeviceViewController: UITableViewController{

 var DeviceNamesArray: NSMutableArray = []
 var titlestring: String!


override func viewDidLoad() {
    super.viewDidLoad()

 //   print(globalImageUrl)

     self.navigationItem.title = titlestring
     let ref = FIRDatabase.database().reference().child("UserDevices")


    ref.queryOrderedByChild("Category").queryEqualToValue(titlestring)
        .observeEventType(.Value, withBlock: { snapshot in

            if let dict = snapshot.value as? NSMutableDictionary{

                //print("dict======print \(dict)")
                for (key,value) in dict {

                    let mainDict = NSMutableDictionary()
                    mainDict.setObject(key, forKey: "userid")
                    if let dictnew = value as? NSMutableDictionary {

                        if let metname = dictnew["DeviceName"] as? String
                        {
                            mainDict.setObject(metname, forKey: "DeviceName")
                        }
                        if let metname = dictnew["Description"] as? String
                        {
                            mainDict.setObject(metname, forKey: "Description")
                        }
                        if let metname = dictnew["Category"] as? String
                        {
                            mainDict.setObject(metname, forKey: "Category")
                        }
                        if let metname = dictnew["ImageUrl"] as? String
                        {
                            mainDict.setObject(metname, forKey: "ImageUrl")

                        }
                        if let metname = dictnew["name"] as? String
                        {
                            mainDict.setObject(metname, forKey: "name")

                        }
                        if let metname = dictnew["phone"] as? String
                        {
                            mainDict.setObject(metname, forKey: "phone")

                        }
                        if let metname = dictnew["city"] as? String
                        {
                            mainDict.setObject(metname, forKey: "city")

                        }
                        if let metname = dictnew["email"] as? String
                        {
                            mainDict.setObject(metname, forKey: "email")

                        }
                    }
                   //print("mainDict========= \(mainDict)")
                    self.DeviceNamesArray.addObject(mainDict)
                }
            }
                  self.tableView.reloadData()
        })
}



override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

    //

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return DeviceNamesArray.count

}
  let storageRef = FIRStorage.storage().reference().child("Devices_Images")

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CategotyDeviceCellViewCell


    if let name = DeviceNamesArray[indexPath.row] as? NSMutableDictionary {
    cell.configureCellone(name["DeviceName"] as! String , Provider: name["name"] as! String , ProviderCity: name["city"] as! String)

        let m = name["ImageUrl"]
        storageRef.dataWithMaxSize(10 * 1024 * 1024, completion: { (data, error) in
            dispatch_async(dispatch_get_main_queue()){
                let postPhoto = UIImage(data: data!)
                cell.CategoryDeviceImage.image = postPhoto
            }

        })


    }



    return cell
}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        dispatch_async(dispatch_get_main_queue()) { [unowned self] in
            let detailsViewController = self.storyboard!.instantiateViewControllerWithIdentifier("DeviceDetailsViewController") as! DeviceDetailsViewController

            if let name = self.DeviceNamesArray[indexPath.row] as? NSMutableDictionary{

                detailsViewController.self.strUserid = name["userid"] as? String
            }

            self.navigationController?.pushViewController(detailsViewController, animated: true)

        }




}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

  }


} //UITableViewController

In the cellForRowAtIndexPath I have added the function for downloading the image but it's for downloading the image from the storage! I could not find the function that download it from the url in the firebase docs!

And what if I don't want to specify the size of it and I just want it to be as the size of the image view I have added in the cell!

AL.
  • 36,815
  • 10
  • 142
  • 281
Mariah
  • 573
  • 3
  • 10
  • 26
  • All you need is to download the image from URL:- http://stackoverflow.com/a/27712427/6297658. Just retrieve the url from database and download from the url. – Dravidian Sep 17 '16 at 13:57
  • Check out http://stackoverflow.com/questions/39548323/how-to-retrieve-image-stored-in-firebase-to-show-it-in-view-image-view/39604874 for some more thoughts on this – Mike McDonald Sep 20 '16 at 22:47

0 Answers0