1

How can I show image when user click on it?

func configureMediaMessageImageView(_ imageView: UIImageView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) {
    switch message.kind {
        case .photo(let photoItem):
            
            /// if we don't have a url, that means it's simply a pending message
            guard let url = photoItem.url else {
                imageView.kf.indicator?.startAnimatingView()
                return
            }
            imageView.kf.indicatorType = .activity
            imageView.kf.setImage(with: url)
            imageView.addTapGesture {
                print("hello")// NOT triggered
            }
            
        
        default:
            break
    }
}


func didTapImage(in cell: MessageCollectionViewCell) {
    print("Hello") //Triggered
}

I don't understand how to open image here!!

here is the codes that I used,

aheze
  • 24,434
  • 8
  • 68
  • 125
Am1rFT
  • 227
  • 5
  • 18

1 Answers1

2

You need to implement MessageCellDelegate. You don't need to add additional gesture recognizers, it has already been handled by MessageKit. Use this method for knowing when user taps the image:

func didTapImage(in cell: MessageCollectionViewCell) {
     guard let indexPath = messagesCollectionView.indexPath(for: cell),
            let message = messagesCollectionView.messagesDataSource?.messageForItem(at: indexPath, in: messagesCollectionView) else {
                return
        }
        if case MessageKind.photo(let media) = message.kind, let imageURL = media.url {
            /// Here is the required url
        }
        print("Image tapped")
}

You can access the image URL from the message object. Now, for showing the image. You will need to create your own GalleryView and pass in the URL

  • thanks for your response, as I said I have implemented it and it is being triggered, but how can I access that image or the url of the image? that is the problem – Am1rFT Aug 29 '21 at 09:22
  • the image url is assigned in "let imageURL = media.url". If your message was created with the image object (instead of the image URL), you can also use "let image = media.image" – Erwan Feb 21 '22 at 04:53