2015-06-24 44 views
0

我正在使用我正在創建的聊天應用程序中的JSQMessagesViewController。我能夠如此成功地發送和接收短信,但也想添加對圖像的支持。如何使用JSQMessagesViewController發送圖片?

我已經實現了didPressAccessoryButton函數,該函數在單擊底部底部回形針/附件按鈕時執行。

我的問題是 - 我不確定接下來要做什麼?或如何實施JSQPhotoMediaItemlink)???

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 

enter image description here

+0

嘗試使用'JSQPhotoMediaItem'只與'JSQMessage(senderId :,顯示名視圖控制器顯示照片:,media:)',它們可能不適合用於混合其他圖書館,圖像本身和其他圖書。 'mediaMsg [「media」]'我想要一個'UIImage'。 – user26830

回答

1

我解析和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"][email protected]"ChatView"; 
     errors[@"function"][email protected]"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]]; 
} 
+0

問題標籤很快,而不是目標-C – Sauron