swiftuiimageviewmessagekit

Open image in MessageKit when user tap


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,


Solution

  • 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