Messagekit: Message/Image Cacheing

Created on 17 Mar 2020  路  15Comments  路  Source: MessageKit/MessageKit

Does Message Kit support Image and Message caching?

Right now every time i load the chat view, my messages are shown immediately, but the images download/load slowly and then pop up in the chat view.

Any solutions to this within MessageKit ?

question

Most helpful comment

@ahmedwasil There is a method on MessagesDisplayDelegate

func configureMediaMessageImageView(_ imageView: UIImageView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView)

I'd try to load images there, something like

func configureMediaMessageImageView(_ imageView: UIImageView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) {
    switch message.kind {
        case .photo(let media):
            imageView.pin_setImage(from: media.url)
        default:
            break
    }
}

All 15 comments

MessageKit does not do any caching of the data / content, that is up to you. AlamoFireImage has an autopurging image cache, or you could roll your own NSCache. I'm sure there are other options out there too, those are just some examples.

Hey @ahmedwasil
Did you have a chance to use the PINRemoteImage library to load your images from the previous issue we discussed?

https://github.com/MessageKit/MessageKit/issues/1276#issuecomment-598036207

PINRemoteImage also provides a cache for your images so it should load faster when you're returning to the chat screen

@martinpucik yes i have tried using the PinRemoteImage but get the same slow response. I have tried the progressive feature too which doesn't seem to work. I got a feeling that I am not setting the image in the correct place.

Currently its all being loaded in this function:

func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle {

I check for the message.kind and if it is a .photo then set a containerView to load the image from a url.

@ahmedwasil There is a method on MessagesDisplayDelegate

func configureMediaMessageImageView(_ imageView: UIImageView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView)

I'd try to load images there, something like

func configureMediaMessageImageView(_ imageView: UIImageView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) {
    switch message.kind {
        case .photo(let media):
            imageView.pin_setImage(from: media.url)
        default:
            break
    }
}

@martinpucik would i still need to set anything in regards to .photo kind in the messageStyle function?

@ahmedwasil not for the image loading part to work

@martinpucik Thanks, but still getting the same response. Images taking a long time to load and re loading each time i go back into the message view.

@ahmedwasil in that case it may be because you're loading large images and the cache is not big enough to hold them all?
In any case it doesn't look like your issue is related to the MessageKit, but if you think it is, could you please try to reproduce it in our Example project?

@martinpucik Is there an example app available which is loading images from a url? Ive looked through the ChatExample App but its using images from the Assets Resources.

@ahmedwasil I added a PR with this option #1294

@martinpucik thank you very much. I will get round to looking at it this weekend. Really appreciate the extra effort.

@ahmedwasil LMK if the example is working for you 馃憤

@martinpucik I have pulled the latest example but can't seem to find your changes. Have they been removed?

@ahmedwasil it鈥檚 in the development branch

@martinpucik thank you that is fully working now. Really appreciate the extra efforts in creating the example.

Quick question can the image be displayed dependant to the ratio of the image or does the size always need to be preset?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asn111 picture asn111  路  3Comments

robertoferraz picture robertoferraz  路  3Comments

NiklasWilson picture NiklasWilson  路  4Comments

NiklasWilson picture NiklasWilson  路  4Comments

bilaalrashid picture bilaalrashid  路  3Comments