使用JSQMessage下载后,很难让媒体替换占位符

时间:2015-03-26 23:02:29

标签: ios objective-c

我正在使用JSQMessage,在显示媒体的占位符时遇到一些困难,直到我正确下载,然后替换为媒体。在将消息和媒体添加到服务器的过程中,我的一切工作正常,我无法让它替换占位符。

目前,我有一个函数可以查询我的数据库并为消息提取一个对象数组,然后循环遍历并为每个对象调用此函数进行输出并将其添加到我的消息线程中。我正在努力弄清楚为什么带有“messageToAdd.isMediaMessage”的部分不会在从服务器下载之后用实际媒体替换占位符。有没有人知道我应该如何处理它以确保它添加一个占位符的消息,然后一旦正确下载媒体后替换?

- (void)addMessage:(PFObject *)object
{
    id<JSQMessageMediaData> messageMedia = nil;
    PFObject *user = object[@"messageSender"];
    [users addObject:user];
    NSString *name = @"";
    if(user[@"profileFName"] && user[@"profileLName"])
        name= [NSString stringWithFormat:@"%@ %@",user[@"profileFName"],user[@"profileLName"]];
    else
        name= [NSString stringWithFormat:@"%@ %@",user[@"consultantFName"],user[@"consultantLName"]];

    if([object[@"messageFileType"] isEqual: @"video"]){
        JSQVideoMediaItem *messageMedia = [[JSQVideoMediaItem alloc] init];
        messageMedia.fileURL = nil;
        messageMedia.isReadyToPlay = NO;
        messageToAdd = [JSQMessage messageWithSenderId:user.objectId displayName:name media:messageMedia];

    } else if ([object[@"messageFileType"] isEqual: @"image"]){
        JSQPhotoMediaItem *messageMedia = [[JSQPhotoMediaItem alloc] init];
        messageMedia.image = nil;
        messageToAdd = [JSQMessage messageWithSenderId:user.objectId displayName:name media:messageMedia];

    } else{
        messageToAdd= [[JSQMessage alloc] initWithSenderId:user.objectId senderDisplayName:name date:object[@"sendDate"] text:object[@"messageContent"]];
    }
    if(isLoadMore)
        [messages insertObject:messageToAdd atIndex:0];
    else
        [messages addObject:messageToAdd];

   // NOT TRIGGERING THESE AFTER MEDIA DOWNLOADED
    if (messageToAdd.isMediaMessage) {
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
            if ([object[@"messageFileType"] isEqual: @"image"]){
                [object[@"messageMedia"] getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
                    if (!error) {
                        JSQPhotoMediaItem *photoItem = [[JSQPhotoMediaItem alloc] initWithImage:[UIImage imageWithData:imageData]];
                        ((JSQPhotoMediaItem *)messageMedia).image = [UIImage imageWithCGImage:photoItem.image.CGImage];
                        [self.collectionView reloadData];
                    }
                }];
            }
            else if([object[@"messageFileType"] isEqual: @"video"]){
                PFFile *videoFile = object[@"messageMedia"];
                NSURL *videoURL = [NSURL URLWithString:videoFile.url];
                ((JSQVideoMediaItem *)messageMedia).fileURL = videoURL;
                ((JSQVideoMediaItem *)messageMedia).isReadyToPlay = YES;
                [self.collectionView reloadData];
            }
            else {
                NSLog(@"%s error: unrecognized media item", __PRETTY_FUNCTION__);
            }
        });
    }
}

2 个答案:

答案 0 :(得分:1)

对于那些提出相同问题/问题的人,我通过查看NotificationChat项目来解决它的工作原理:https://github.com/relatedcode/NotificationChat/blob/master/NotificationChat/Classes/Chat/ChatView.m。它给出了使用JSQMessage平台的非常好的概述。

这是我修改过的功能,因此您可以看到成品。

- (void)addMessage:(PFObject *)object
{
    PFObject *user = object[@"messageSender"];
    [users addObject:user];

    PFFile *mediaMessage = object[@"messageMedia"];

    NSString *name = @"";
    if(user[@"profileFName"] && user[@"profileLName"])
        name= [NSString stringWithFormat:@"%@ %@",user[@"profileFName"],user[@"profileLName"]];
    else
        name= [NSString stringWithFormat:@"%@ %@",user[@"consultantFName"],user[@"consultantLName"]];

    if([object[@"messageFileType"] isEqual: @"video"]){
        JSQVideoMediaItem *mediaItem = [[JSQVideoMediaItem alloc] initWithFileURL:[NSURL URLWithString:mediaMessage.url] isReadyToPlay:YES];
        mediaItem.appliesMediaViewMaskAsOutgoing = [user.objectId isEqualToString:self.senderId];
        messageToAdd = [[JSQMessage alloc] initWithSenderId:user.objectId senderDisplayName:name date:object.createdAt media:mediaItem];

    } else if ([object[@"messageFileType"] isEqual: @"image"]){
        JSQPhotoMediaItem *mediaItem = [[JSQPhotoMediaItem alloc] initWithImage:nil];
        mediaItem.appliesMediaViewMaskAsOutgoing = [user.objectId isEqualToString:self.senderId];
        messageToAdd = [[JSQMessage alloc] initWithSenderId:user.objectId senderDisplayName:name date:object.createdAt media:mediaItem];

        [mediaMessage getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error)
         {
             if (error == nil)
             {
                 mediaItem.image = [UIImage imageWithData:imageData];
                 [self.collectionView reloadData];
             }
         }];


    } else{
        messageToAdd= [[JSQMessage alloc] initWithSenderId:user.objectId senderDisplayName:name date:object[@"sendDate"] text:object[@"messageContent"]];
    }
    if(isLoadMore)
        [messages insertObject:messageToAdd atIndex:0];
    else
        [messages addObject:messageToAdd];

}

答案 1 :(得分:0)

基于代码,我认为一个可能的原因是在成功下载数据并在后台线程上异步下载主要(UI)线程后需要reloadData