XMPP 发送图片,不显示解决方案

上篇文章中是将图片压缩成为 NSData 数据进行进行传送的,但是图片好友那边不能将 NSData 转为原始图片

解决方案:将图片发送给一个文件服务器,在文件服务器有一个 URL 地址,再将这个 URL 地址发送给 Openfire 服务器,好友那边得到的是 图片的 URL 地址,根据 URL 地址从文件服务器中去下载图片

上代码:
在 AppDelegate.h类中添加两个属性

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

聊天界面的消息

@interface MeTableViewController ()<NSFetchedResultsControllerDelegate>

// 消息的结果集

@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) {

    }];

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值