如何使用 JSQMessagesViewController 发送图片?

How to send Pictures using JSQMessagesViewController?

我正在制作的聊天应用程序中使用 JSQMessagesViewController。我能够如此成功地发送和接收短信,但也想添加对图像的支持。

我已经实现了 didPressAccessoryButton 功能,当点击底部回形针/附件按钮时执行。

我的问题是 - 我不确定下一步该做什么?或者如何实现 JSQPhotoMediaItem (link)???

override func didPressAccessoryButton(sender: UIButton!) {
    var imgToSend = UIImage(named: "devassets_avatar_01_selected.png")
    var photoMediaItem = JSQPhotoMediaItem(image: imgToSend)


    let mediaMsg = PFObject(className: "Message")
    mediaMsg["content"] = "this is photo item"
    mediaMsg["room"] = room
    mediaMsg["user"] = PFUser.currentUser()
    mediaMsg["media"] = photoMediaItem

    mediaMsg.saveInBackgroundWithBlock { (success, error) -> Void in
        if error == nil {
            //self.loadMessages()

            self.room["lastUpdate"] = NSDate()
            self.room.saveInBackgroundWithBlock(nil)

        }else{
            println("error sending message \(error!.localizedDescription)")
        }

    }

    self.finishSendingMessage()
}

当上述函数为 运行 时,应用程序崩溃并显示以下堆栈跟踪:

2015-06-24 09:58:33.850 ShitTalk[9530:818302] *** Terminating app due   to uncaught exception 'NSInvalidArgumentException', reason: 'PFObject values may not have class: JSQPhotoMediaItem'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000105cb5c65 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010594ebb7 objc_exception_throw + 45
2   CoreFoundation                      0x0000000105cb5b9d +[NSException raise:format:] + 205
3   ShitTalk                            0x000000010393e7f1 -[PFObject _setObject:forKey:onlyIfDifferent:] + 750
4   ShitTalk                            0x000000010393e4f0 -[PFObject setObject:forKey:] + 53
5   ShitTalk                            0x000000010393e90f -[PFObject setObject:forKeyedSubscript:] + 50
6   ShitTalk                            0x0000000103909e4a _TFC8ShitTalk22MessagesViewController23didPressAccessoryButtonfS0_FGSQCSo8UIButton_T_ + 1306
7   ShitTalk                            0x000000010390a0f6 _TToFC8ShitTalk22MessagesViewController23didPressAccessoryButtonfS0_FGSQCSo8UIButton_T_ + 54
8   ShitTalk                            0x000000010389866d -[JSQMessagesViewController messagesInputToolbar:didPressLeftBarButton:] + 125
9   ShitTalk                            0x000000010387ebfd -[JSQMessagesInputToolbar jsq_leftBarButtonPressed:] + 93
10  UIKit                               0x00000001062dfda2 -[UIApplication sendAction:to:from:forEvent:] + 75
11  UIKit                               0x00000001063f154a -[UIControl _sendActionsForEvents:withEvent:] + 467
12  UIKit                               0x00000001063f0919 -[UIControl touchesEnded:withEvent:] + 522
13  UIKit                               0x000000010668aa10 _UIGestureRecognizerUpdate + 9487
14  UIKit                               0x000000010632c686 -[UIWindow _sendGesturesForEvent:] + 1041
15  UIKit                               0x000000010632d2b2 -[UIWindow sendEvent:] + 666
16  UIKit                               0x00000001062f3581 -[UIApplication sendEvent:] + 246
17  UIKit                               0x0000000106300d1c _UIApplicationHandleEventFromQueueEvent + 18265
18  UIKit                               0x00000001062db5dc _UIApplicationHandleEventQueue + 2066
19  CoreFoundation                      0x0000000105be9431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
20  CoreFoundation                      0x0000000105bdf2fd __CFRunLoopDoSources0 + 269
21  CoreFoundation                      0x0000000105bde934 __CFRunLoopRun + 868
22  CoreFoundation                      0x0000000105bde366 CFRunLoopRunSpecific + 470
23  GraphicsServices                    0x000000010a368a3e     GSEventRunModal + 161
24  UIKit                               0x00000001062de900     UIApplicationMain + 1282
25  ShitTalk                            0x00000001038afe57 main + 135
26  libdyld.dylib                       0x0000000107fda145 start + 1 
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我用 Parse 和 JSQMessages 成功地做到了,但是在 ObjectiveC 中。请查看我的代码版本,看看是否有帮助?

- (void)sendMessage:(NSString *)text Picture:(UIImage *)picture
{

PFObject *object = [PFObject objectWithClassName:PF_CHAT_CLASS_NAME];
object[PF_CHAT_USER] = [PFUser currentUser];
object[PF_CHAT_ROOMID] = roomId;
object[PF_CHAT_TEXT] = text;

// picture object from UIImagePickerControllerDelegate
NSData *imageData = UIImageJPEGRepresentation(picture, 1.0);
PFFile *imageFile = [PFFile fileWithName:@"chat_img.jpg" data:imageData];

[object setObject:imageFile forKey:PF_CHAT_PICTURE];

// Set the access control list to current user for security purposes
object.ACL = [PFACL ACLWithUser:[PFUser currentUser]];




[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
    if (error == nil)
    {
        [JSQSystemSoundPlayer jsq_playMessageSentSound];
        [self loadMessages];
    }
    else
    {
        PFObject *errors=[PFObject objectWithClassName:@"ShagErrors"];
        errors[@"errorno"]=[NSString stringWithFormat:@"%ld",(long)error.code];
        errors[@"erDescription"]=error.description;
        errors[@"className"]=@"ChatView";
        errors[@"function"]=@"sendMessage";
        [errors saveInBackground];
        [ProgressHUD showError:@"Network error."];;

    }
}];

[self finishSendingMessage];

}

这是我加载它的方式

if(object[PF_CHAT_PICTURE])
{
    UIImageView *imageView = [[UIImageView alloc] init];
    NSString *imgUrl = [object[PF_CHAT_PICTURE] valueForKey:@"url"];

    UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgUrl];
    if(image == nil)
    {
        NSURL *imageUrl = [NSURL URLWithString:imgUrl];

        [imageView setImageWithURL:imageUrl completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
        {
            JSQPhotoMediaItem *mediaItem = [[JSQPhotoMediaItem alloc] initWithImage:image];
            JSQMessage *message = [[JSQMessage alloc] initWithSenderId:user.objectId senderDisplayName:user[PF_USER_FULLNAME] date:object.createdAt media:mediaItem];
            [messages addObject:message];
        }];
    }
    else
    {
        JSQPhotoMediaItem *mediaItem = [[JSQPhotoMediaItem alloc] initWithImage:image];
        message = [[JSQMessage alloc] initWithSenderId:user.objectId senderDisplayName:user[PF_USER_FULLNAME] date:object.createdAt media:mediaItem];
    }
}
else
{
    message = [[JSQMessage alloc] initWithSenderId:user.objectId senderDisplayName:user[PF_USER_FULLNAME] date:object.createdAt text:object[PF_CHAT_TEXT]];
}