Hi!
i Want to show my images in fullscreen, from
func didTapMessage(in cell: MessageCollectionViewCell)
So i need to have access to MediaMessageImageView from this method.
How can i do this ? Thanks
@JulienLevallois
You can use UICollectionView's method indexPath(for: UICollectionViewCell) to get the IndexPath for the MessageCollectionViewCell argument. Using this IndexPath, you can then get the MessageType for this cell through the MessagesDataSource method messageForItem(at:IndexPath,in:MessagesCollectionView).
func didTapMessage(in cell: MessageCollectionViewCell) {
guard let indexPath = messagesCollectionView.indexPath(for: cell) else { return }
guard let messagesDataSource = messagesCollectionView.messagesDataSource else { return }
let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
}
Since we also give you the cell as a parameter, you can cast to the MediaMessageCell and access the media imageView
Okay thanks @SD10
This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Hey, I am using this way to get the user too, but I can't get the message.kind value.
It says that the MediaItem exist and give me the memory address, but when I am trying to cast it and use the image, it says nil
if let indexPath = messagesCollectionView.indexPath(for: cell),
let messagesDataSource = messagesCollectionView.messagesDataSource {
let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
let sender = message.sender
let picture = message.kind as? PhotoItem // Here the value is nil .. :( it is normal, the compiler is telling me that it will always fail, but then I don't understand how to get my value 馃槄
}
How do I access to media item contained in message.kind ?
Do you have any idea ?
@gorbat-o You want the associated value of message.kind, not message.kind itself
Hey @SD10, thank you for the quick answer.
I would like to get the media item contained in message.kind .. I don't understand how to unwrap the value
Hey @gorbat-o. To help clarify what @SD10 is getting at, I suggest reading over the Associated Values section in the Enumerations chapter of the Swift Programming Guide.
I read this already, but reading it again made me thing about the way to get the value.
Thank you
i must be tired .. haha
Have a good night (it is almost midnight here)
If someone is still scratching there head like me.
here is meaning to get associated value from the message
switch message.kind {
case .photo(let photoItem):
if let image = photoItem.image{
// here is your image
}
default:
break
}
Note -
Please read the above link for associated type before just writting down this code.
This issue helped me at least recognize that a message has an image or text in it, but I the imageTapped function is not enlarging the image. Any clues? Running on Swift 5 & iOS 13, heres my code:
extension RoomVC: MessageCellDelegate {
func didTapMessage(in cell: MessageCollectionViewCell) {
guard let indexPath = messagesCollectionView.indexPath(for: cell) else { return }
guard let messagesDataSource = messagesCollectionView.messagesDataSource else { return }
let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
switch message.kind {
case .photo(let photoItem):
log.info("Message is a photo.")
if let img = photoItem.image{
imageTapped(image: img)
}
default:
log.info("Message is not a photo.")
break
}
}
func imageTapped(image: UIImage){
let newImageView = UIImageView(image: image)
newImageView.frame = UIScreen.main.bounds
newImageView.backgroundColor = .black
newImageView.contentMode = .scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
newImageView.addGestureRecognizer(tap)
self.view.addSubview(newImageView)
self.navigationController?.isNavigationBarHidden = true
self.tabBarController?.tabBar.isHidden = true
}
@objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
self.navigationController?.isNavigationBarHidden = false
self.tabBarController?.tabBar.isHidden = false
sender.view?.removeFromSuperview()
}
}
This issue helped me at least recognize that a message has an image or text in it, but I the imageTapped function is not enlarging the image. Any clues? Running on Swift 5 & iOS 13, heres my code:
extension RoomVC: MessageCellDelegate { func didTapMessage(in cell: MessageCollectionViewCell) { guard let indexPath = messagesCollectionView.indexPath(for: cell) else { return } guard let messagesDataSource = messagesCollectionView.messagesDataSource else { return } let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView) switch message.kind { case .photo(let photoItem): log.info("Message is a photo.") if let img = photoItem.image{ imageTapped(image: img) } default: log.info("Message is not a photo.") break } } func imageTapped(image: UIImage){ let newImageView = UIImageView(image: image) newImageView.frame = UIScreen.main.bounds newImageView.backgroundColor = .black newImageView.contentMode = .scaleAspectFit newImageView.isUserInteractionEnabled = true let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage)) newImageView.addGestureRecognizer(tap) self.view.addSubview(newImageView) self.navigationController?.isNavigationBarHidden = true self.tabBarController?.tabBar.isHidden = true } @objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) { self.navigationController?.isNavigationBarHidden = false self.tabBarController?.tabBar.isHidden = false sender.view?.removeFromSuperview() } }
@douglasrcjames try self.view.bringSubview(toFront: newImageView).
I would suggest to use a controller with an image-view and add that as a child controller when you need to show the image.
that's much cleaner and re-usable.
@pawankmrai I put a print statement in the imageTapped function to see if it was ever even entered, and it wasn't, so that bringSubviewtofront function isnt going to trigger either. I will try adding a controller with an image-view in it, thanks for the suggestion!
Most helpful comment
If someone is still scratching there head like me.
here is meaning to get associated value from the message
Note -
Please read the above link for associated type before just writting down this code.