XMPP 發送圖片,不顯示解決方案

上篇文章中是將圖片壓縮成爲 NSData 數據進行進行傳送的,可是圖片好友那邊不能將 NSData 轉爲原始圖片web

解決方案:將圖片發送給一個文件服務器,在文件服務器有一個 URL 地址,再將這個 URL 地址發送給 Openfire 服務器,好友那邊獲得的是 圖片的 URL 地址,根據 URL 地址從文件服務器中去下載圖片數組

上代碼:
在 AppDelegate.h類中添加兩個屬性ruby

@property (strong,nonatomic) XMPPMessageArchiving * msgArchiving;
@property (strong,nonatomic) XMPPMessageArchivingCoreDataStorage * msgArchivingStorage;

在AppDelegate.m類中註冊消息模塊

(void)connect{
    // 5.添加消息模塊
    self.msgArchivingStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
    self.msgArchiving = [[XMPPMessageArchiving alloc] initWithMessageArchivingStorage:self.msgArchivingStorage];
    [self.msgArchiving activate:self.xmppStream];
}

釋放資源服務器

- teardownStream{
    [self.msgArchiving deactivate];
    self.msgArchiving = nil;
    self.msgArchivingStorage = nil;
}

聊天界面的消息app

@interface MeTableViewController ()<NSFetchedResultsControllerDelegate>

// 消息的結果集svg

@property (strong,nonatomic) NSFetchedResultsController * resultsController;

@end

/** * 得到消息的結果集 */
- (void)loadChat
{
    // 上下文
    NSManagedObjectContext * msgContext = [[AppDelegate alloc] init].msgArchivingStorage.mainThreadManagedObjectContext;

    // 執行查詢
    NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"XMPPMessageArchiving_Message_CoreDataObject"];

    // 設置過濾
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"streamBareJidStr = %@ AND bareJidStr = %@",loginUserJid,friendJid.bare];
    request.predicate = pre;

    // 設置排序
    NSSortDescriptor * timeSort = [NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:YES];
    request.sortDescriptors = @[timeSort];

    // 執行請求
    self.resultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:msgContext sectionNameKeyPath:nil cacheName:nil];
    self.resultsController.delegate = self;

    NSError * error = nil;
    [self.resultsController performFetch:&error];
    if (error) {
        NSLog(@"%@",[[error userInfo] description]);
    }

}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // 結果集數組中的元素
    return self.resultsController.fetchedObjects.count;
}

/** * 獲取消息的列表 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * ID = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];

    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }

    // 獲去聊天消息對象
    XMPPMessageArchiving_Message_CoreDataObject * msgObj = self.resultsController.fetchedObjects[indexPath.row];
    // 消息在 body 中
    cell.textLabel.text = msgObj.body;
    // 獲取原始 xml數據
    XMPPMessage * message = msgObj.message;

    // 獲取附件的類型
    NSString * bodyType = [message attributeStringValueForName:@"bodyType"];

    if ([bodyType isEqualToString:@"image"]) {
        // 獲取文件的路徑
        NSString* url = msgObj.body;

        // 顯示圖片
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:@"ihead__007"];
    }

    return cell;
}

/** * 發送消息 */
- (void)sendmsg:(NSString*)msgText
{
    XMPPMessage *msg = [XMPPMessage messageWithType:@"chat" to:self.friendJid];
    // 消息在 body 中
    [msg addBody:msgText];
    // 發送到服務器
    [[[AppDelegate alloc] init].xmppStream sendElement:msg];
}


/** * 思路:發圖片上傳到文件服務器,將文件在服務器的 url 發給 openfire 服務器,好友接受到的是 url,而後根據 url 獲取圖片,這樣就能解決圖片不顯示的問題 */
- (void)sendImage:(UIImage*)image
{
    // 定義圖片的名字:user + 時間
    NSDateFormatter * dateForm = [[NSDateFormatter alloc] init];
    dateForm.dateFormat = @"yyyyMMddHHmmss";
    NSString *currentTimeStr = [dateForm stringFromDate:[NSDate date]];
    NSString *LoginUser;
    NSString * fileName = [LoginUser stringByAppendingString:currentTimeStr];

    // 拼接上傳文件的路徑
    NSString * url;
    NSString *uploadPath = [url stringByAppendingString:fileName];

    // 請求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

    // 請求的參數
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"url"] = uploadPath;
    // 發送請求
    [mgr POST:uploadPath parameters:param success:^ void(AFHTTPRequestOperation * operation, id responseObject) {

        XMPPMessage *msg = [XMPPMessage messageWithType:@"chat" to:self.friendJid];
        [msg addAttributeWithName:@"bodyType" stringValue:@"image"];
        [msg addBody:uploadPath];

        [[[AppDelegate alloc] init].xmppStream sendElement:msg];


    } failure:^ void(AFHTTPRequestOperation * operation, NSError * error) {

    }];

}