从JSQMessagesViewController打开图像

Rob*_*ert 3 ios jsqmessagesviewcontroller

我正在使用JSQMesssagesViewController来构建消息传递应用程序.我现在可以使用此图像发送图像,但想点击图像以全屏打开.此功能类似于标准消息传递应用程序,允许您点击图像"气泡",并捏放大和缩小.有没有人有过使用JSQMessagesViewController做这个的经验?感谢那些可以提供帮助的人!

小智 12

SWIFT 3:我发现了另一种不使用任何其他pod /库的方法.

1)添加var selectedImage: UIImage?到您的ChatViewController.

2)覆盖方法didTapMessageBubbleAt,例如:

override func collectionView(_ collectionView: JSQMessagesCollectionView!, didTapMessageBubbleAt indexPath: IndexPath!) {
    if let test = self.getImage(indexPath: indexPath) {
        selectedImage = test
        self.performSegue(withIdentifier: "showMedia", sender: self)
    }
}
Run Code Online (Sandbox Code Playgroud)

3)添加此函数以在用户点击图像时返回图像并返回UIImage(如果是已粘贴的文本消息,则返回nil):

func getImage(indexPath: IndexPath) -> UIImage? {
        let message = self.messages[indexPath.row]
        if message.isMediaMessage == true {
            let mediaItem = message.media
            if mediaItem is JSQPhotoMediaItem {
                let photoItem = mediaItem as! JSQPhotoMediaItem
                if let test: UIImage = photoItem.image {
                    let image = test
                    return image
                }
            }
        }
        return nil
    }
Run Code Online (Sandbox Code Playgroud)

4)添加segue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showMedia" {
            if let pageDeDestination = segue.destination as? ShowMediaViewController {
                pageDeDestination.image = selectedImage!
                } else {
                print("type destination not ok")
            }
        } else {
            print("segue inexistant")
        }
    }
Run Code Online (Sandbox Code Playgroud)

5)在Interface Builder中,在您附近添加一个View控制器,chatVC并添加一个segue(type Show)showMedia作为segue标识符.在新的中添加图像视图viewcontroller.

6)这是我的代码ShowMediaViewController:

class ShowMediaViewController: UIViewController {
    var image: UIImage? = nil
    var titreText: String!

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var titre: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()

        //titre.text = titreText

        if image != nil {
            imageView.image = image
        } else {
            print("image not found")
        }

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.
    }
}
Run Code Online (Sandbox Code Playgroud)

不要忘记将"Aspect Fit"作为参数放在图像视图中(在界面构建器中)以正确显示图像.


Bes*_*sat 10

JSQmessage没有处理,但您可以在此方法中使用zoomPopup类添加此功能:

- (void)collectionView:(JSQMessagesCollectionView *)collectionView didTapMessageBubbleAtIndexPath:(NSIndexPath *)indexPath
{
    JSQMessage *message = [self.messageModelData.messages objectAtIndex:indexPath.row];

    if (message.isMediaMessage) {
        id<JSQMessageMediaData> mediaItem = message.media;

        if ([mediaItem isKindOfClass:[JSQPhotoMediaItem class]]) {

            NSLog(@"Tapped photo message bubble!");

            JSQPhotoMediaItem *photoItem = (JSQPhotoMediaItem *)mediaItem;
            [self popupImage:photoItem.image];
        }
    }
}

- (void) popupImage: (UIImage*)image
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *topView = window.rootViewController.view;
    imageView = [[UIImageView alloc] initWithImage:image];

    zoomPopup  *popup = [[zoomPopup alloc] initWithMainview:topView andStartRect:CGRectMake(topView.frame.size.width/2, topView.frame.size.height/2, 0, 0)];
    [popup showPopup:imageView];
}
Run Code Online (Sandbox Code Playgroud)

你可以在这里看到zoomPopup:https: //github.com/Tintenklecks/zoomPopup