I want to get a saved Image in Firebase Storage.
I have the url saved in an string value in the Firebase database:
mediaUrl = url!.absoluteString
Now I want to get this image.
I observe the messages.
func observeMessages() {
let query = Constants.refs.databaseChats.child(chatId).queryLimited(toLast: 50)
_ = query.observe(.childAdded, with: { [weak self] snapshot in
if let data = snapshot.value as? [String: String],
let id = data["sender_id"],
let name = data["name"],
let timestamp = data["timestamp"],
let media = data["media"],
let text = data["text"]?.encode(),
!text.isEmpty
{
if media == "text" {
if let message = JSQMessage(senderId: id, senderDisplayName: name, date: self!.dateFormatter.date(from: timestamp), text: text) {
self?.messages.append(message)
self?.finishReceivingMessage()
}
}
else if media == "image" {
let storageRef = Storage.storage().reference(withPath: text)
storageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) -> Void in
if data != nil {
let image = UIImage(data: data!)
if let imageMessage = JSQMessage(senderId: id, senderDisplayName: name, date: self!.dateFormatter.date(from: timestamp), media: image as! JSQMessageMediaData) {
self?.messages.append(imageMessage)
print("image message")
self?.finishReceivingMessage()
}
}
}
}
}
})
}
If the media is an image, I want to create an image bubble, and when text a normal text bubble. But I get no Image in the JSQMEssageViewController. What went wrong? Can someone help me there?
I get the url in console:
https://firebasestorage.googleapis.com/v0/b/emmessenger-22.appspot.com/o/media%2FJzl0EUmjSvZZcpCd8Mdi8X9q87G2%2F03.06.2019%2021:26:39?alt=media&token=1c45feaf-0ed8-4f88-9c1f-13f4c6a8f3d4
And after that I get the following at console:
2019-06-03 21:28:21.092580+0200 EMMessenger[38502:554129] [] nw_proxy_resolver_create_parsed_array PAC evaluation error: NSURLErrorDomain: -1003
Thanks
Actually you don't need to call getData
function for this case. As you're using tableView
so I would recommend to use SDWebImage library to load the images on cells asynchronously. Once you have the imageURL
just call below method
yourImageView.sd_setImage(with: URL(string: "https://firebasestorage.googleapis.com/v0/b/emmessenger-22.appspot.com/o/media%2FJzl0EUmjSvZZcpCd8Mdi8X9q87G2%2F03.06.2019%2021:26:39?alt=media&token=1c45feaf-0ed8-4f88-9c1f-13f4c6a8f3d4"), placeholderImage: UIImage(named: "placeholder.png"))
Rest the library will do for you.