0
votes

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
}
}
2
What's wrong? Is profileImage a valid Base64 String? Are images saved really using Base64 encoding?Larme
I saved image in storge from jpeg to dataSemarY
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: stackoverflow.com/questions/24231680/…Galo Torres Sevilla

2 Answers

0
votes

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!)
    }
0
votes

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.