0

User upload image to firebase storage and the url string is added to the firebase database, How can I take the string in the firebase database and convert it to uiimage to use it as an image ?

The string returned from database is like this:

https://firebasestorage.googleapis.com/v0/b/ichatmessage.appspot.com/o/YNoodTnOSGXl6HkPoqhOPAopb8v1?alt=media&token=738771a5-53e3-46c2-b508-ad5cfa03e74e

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! NewMessageCell
    let user = users[indexPath.row]
    cell.textLabel?.text = user.username
    cell.detailTextLabel?.text = user.email
    let currentID = Auth.auth().currentUser?.uid
    let databaseRef = Database.database().reference().child("users").child(currentID!)
    databaseRef.observe(.value) { (data) in
        let dictinoray = data.value as? [String: AnyObject]
        let profileImage = dictinoray!["profileimage"] as! String
        cell.imageView?.image = profileImage.toImage()
        print(profileImage)
    }
    return cell
}

extention

  extension String {
func toImage() -> UIImage? {
    if let data = Data(base64Encoded: self, options: .ignoreUnknownCharacters){
        return UIImage(data: data)
    }
    return nil
}
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
SemarY
  • 49
  • 1
  • 9
  • 1
    What's wrong? Is `profileImage` a valid Base64 String? Are images saved really using Base64 encoding? – Larme Jan 03 '19 at 10:23
  • I saved image in storge from jpeg to data – SemarY Jan 03 '19 at 10:37
  • 2
    Of course you cannot do that. You have URL pointing to the image, not the image encoded. what you need is to get the image from URL. Check here: https://stackoverflow.com/questions/24231680/loading-downloading-image-from-url-on-swift – Galo Torres Sevilla Jan 03 '19 at 10:44

2 Answers2

0

I used other way by mapping and using storage downloadurl and it worked

          override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! NewMessageCell
    let user = users[indexPath.row]
    cell.textLabel?.text = user.username
    cell.detailTextLabel?.text = user.email
        let storageref = Storage.storage().reference().child(user.userid)
    storageref.downloadURL { (imgurl, er) in
        let imagedata = try? Data.init(contentsOf: imgurl!)
        cell.imageView?.layer.cornerRadius = (cell.imageView?.frame.size.width)!/2
        cell.imageView?.image = UIImage.init(data: imagedata!)
    }
SemarY
  • 49
  • 1
  • 9
0

Another option would be to load and cache the image through Kingfisher. It's how I would usually load images within a tableView that's from an API.

import Kingfisher above your class.

Then in your cellForRowAtIndexPath, set the image with Kingfisher.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! NewMessageCell
    let user = users[indexPath.row]
    cell.textLabel?.text = user.username
    cell.detailTextLabel?.text = user.email
    let currentID = Auth.auth().currentUser?.uid
    let databaseRef = Database.database().reference().child("users").child(currentID!)
    databaseRef.observe(.value) { (data) in
        let dictinoray = data.value as? [String: AnyObject]
        let profileImage = dictinoray!["profileimage"] as! String
        cell.imageView?.kf.setImage(with: URL(string: profileImage))
    }
    return cell
}

Not sure if you were looking for an alternative but that's another way to do it.

Jason
  • 209
  • 2
  • 13