Загрузка строки изображения профиля из базы данных Firebase в uiimage - PullRequest
0 голосов
/ 03 января 2019

Пользователь загружает изображение в хранилище Firebase, и строка URL добавляется в базу данных Firebase. Как я могу взять строку в базе данных Firebase и преобразовать ее в uiimage, чтобы использовать ее в качестве изображения?

Строкавозвращается из базы данных так:

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
}

расширение

  extension String {
func toImage() -> UIImage? {
    if let data = Data(base64Encoded: self, options: .ignoreUnknownCharacters){
        return UIImage(data: data)
    }
    return nil
}
}

Ответы [ 2 ]

0 голосов
/ 09 января 2019

Другим вариантом будет загрузка и кеширование изображения через Kingfisher.Именно так я обычно загружаю изображения в tableView из API.

import Kingfisher выше вашего класса.

Затем в вашем cellForRowAtIndexPath установите изображение с помощью 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
}

Не уверен, что вы искали альтернативуно это еще один способ сделать это.

0 голосов
/ 03 января 2019

Я использовал другой способ сопоставления и использования downloadurl хранилища, и это сработало

          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!)
    }
...