How to send Pictures using JSQMessagesViewController?

China☆狼群 提交于 2019-12-12 02:53:13

问题


I am using the JSQMessagesViewController in a Chat App I am making. I am able so successfully send and receive text messages but would like to add support for Images also.

I have implemented the didPressAccessoryButton function which is executed when the lower bottom paper clip / attachment button is clicked.

My question is - I am not sure what to do next? or how to implement the 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()
}

When the above function is run, the app crashes with the following stacktrace:

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


回答1:


I did it successfully with Parse and JSQMessages but in ObjectiveC. Please look at my version of code and see if this is of any help?

- (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];

}

And here is how I am loading it back

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]];
}


来源:https://stackoverflow.com/questions/31018904/how-to-send-pictures-using-jsqmessagesviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!