如何将图像和文本一起发送到xmpp服务器?

时间:2013-07-12 13:53:04

标签: iphone objective-c

我已经通过以下代码发送了短信和图片,但我想将它们一起发送,如何管理呢?

-(void)myImage
{

NSString *strMessage;

UIImage *image = [UIImage imageNamed:@"bg_i5.png"];
//  NSString *path = [[NSBundle mainBundle] pathForResource:@"kamlesh" ofType:@"JPG"];

if(image){
    //NSData *dataObj = UIImagePNGRepresentation(image);

    NSData *dataObj = UIImageJPEGRepresentation(image, 0);
    //     UIImage *image = [UIImage imageWithData:dataObj];
    //   NSError *error;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"image"]; //Add the file name

    [dataObj writeToFile:filePath atomically:YES];



    NSData *plainTextData = [filePath dataUsingEncoding:NSUTF8StringEncoding];
    strMessage = [plainTextData base64EncodedString];

    // strMessage =  [dataObj base64Encoding];

} else {
    strMessage =  @"";
}




NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:@"image"];

NSXMLElement *attachment = [NSXMLElement elementWithName:@"attachment"];
[attachment setStringValue:strMessage];


NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"type" stringValue:@"chat"];
[message addAttributeWithName:@"to" stringValue:@"12598221@test-desktop"];  //1313112 878899100
[message addChild:body];
[message addChild:attachment];


[[self xmppStream] sendElement:message];
}

-(void)SenMessage

{ 
NSString *messageStr = sendMessagetxt.text;

if([messageStr length] > 0) {



    UserDetails *details=[UserDetails new];

    NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
    [body setStringValue:messageStr];

    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];
    [message addAttributeWithName:@"to" stringValue:@"12598221@test-desktop"];
    [message addChild:body];
    details.JUserbody=messageStr;
    details.JUserName=@"123456789@shrikant-desktop";
    details.messStatus=@"1";
    [self.xmppStream sendElement:message];
    [app.AllContactUser addObject:details];
    [details release];

    XMPPJID *jid = [XMPPJID jidWithString:chatWithUser];

    NSLog(@"Attempting TURN connection to %@", jid);
     }

sendMessagetxt.text=@"";

}

如何以单体形式发送文字和图像?

感谢

2 个答案:

答案 0 :(得分:-1)

NSData *dataF = UIImagePNGRepresentation(SendImage);
NSString *imgStr=[dataF base64Encoding];

NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:messageStr];

 NSXMLElement *ImgAttachement = [NSXMLElement elementWithName:@"attachement"];
[ImgAttachement setStringValue:imgStr];

NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"type" stringValue:@"chat"];
[message addAttributeWithName:@"to" stringValue:chatWithUser];
[message addChild:body];
[message addChild:ImgAttachement];


[self.xmppStream sendElement:message]; 

答案 1 :(得分:-1)

NSString *messageStr = @”This is a test”;
UIImage *imagePic = [UIImage imageNamed:@"SmallPaul.png"];
if([messageStr length] > 0 || [imagePic isKindOfClass:[UIImage class]] )
{
    NSXMLElement *body = [NSXMLElementelementWithName:@"body"];
    [body setStringValue:messageStr];
    NSMutableDictionary *m = [[NSMutableDictionaryalloc] init];
    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type"stringValue:@"chat"];
    [message addAttributeWithName:@"to"stringValue:self.userInfo];
    [message addChild:body];
    if([imagePic isKindOfClass:[UIImage class]])
    {
        [m setObject:imagePic forKey:@"image"];
        NSData *dataPic =  UIImagePNGRepresentation(imagePic);
        NSXMLElement *photo = [NSXMLElement elementWithName:@"PHOTO"];
        NSXMLElement *binval = [NSXMLElement elementWithName:@"BINVAL"];
        [photo addChild:binval];
        NSString *base64String = [dataPic base64EncodedStringWithOptions:0];
        [binval setStringValue:base64String];
        [message addChild:photo];
    }
    [xmppStream sendElement:message];
    }
}