Recently I have been developing a chat application and was having trouble asynchronously loading images with the chat.
Working with MessageKit - 2.0
I had tried this
import MessageKit
class Image: MediaItem {
var url: URL?
var image: UIImage?
var placeholderImage: UIImage
var size: CGSize
init(url: URL) {
self.url = url
self.size = CGSize(width: 240, height: 240)
self.placeholderImage = UIImage()
DispatchQueue.global().async {
if let data = try? Data(contentsOf: url) {
if let image = UIImage(data: data) {
DispatchQueue.main.async {
self.image = image
}
}
}
}
}
init(image: UIImage) {
self.image = image
self.size = CGSize(width: 240, height: 240)
self.placeholderImage = UIImage()
}
}
Then I initialize the Image MessageType
with kind = .photo(Image(url: url))
This doesn't seem to work.
messageImage
property toMessage
, so once the image is downloaded I stored it there and then check for that property to avoid unnecessary request to download the image. – Breda