【问题标题】:Open image in MessageKit when user tap用户点击时在 MessageKit 中打开图像
【发布时间】:2021-08-29 04:41:06
【问题描述】:

当用户点击图片时如何显示?

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
}

这里的图片我不明白怎么打开!!

这是我使用的代码,

【问题讨论】:

    标签: swift uiimageview messagekit


    【解决方案1】:

    您需要实现 MessageCellDelegate。您不需要添加额外的手势识别器,它已经由 MessageKit 处理。使用此方法了解用户何时点击图像:

    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")
    }
    

    您可以从消息对象访问图像 URL。 现在,用于显示图像。您需要创建自己的 GalleryView 并传入 URL

    【讨论】:

    • 感谢您的回复,正如我所说我已经实现它并且它正在被触发,但是我怎样才能访问该图像或图像的 url?这就是问题
    • 图像 url 在“let imageURL = media.url”中分配。如果您的消息是使用图像对象(而不是图像 URL)创建的,您还可以使用“let image = media.image”
    猜你喜欢
    • 2021-04-27
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多