I am working on the chat app in which I have used the xmpp framework. I am able to send or receive the messages through xmpp. But when I tried to upload the user profile or avatar image with the following code, didn't get success.
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringPath]];
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
UIImageView *thumbimage;
thumbimage.image = tmpImage;
NSData *imageData1 = UIImageJPEGRepresentation(thumbimage.image,0.5);
NSXMLElement *vCardXML = [NSXMLElement elementWithName:@"vCard" xmlns:@"vcard-temp"];
NSXMLElement *photoXML = [NSXMLElement elementWithName:@"PHOTO"];
NSXMLElement *typeXML = [NSXMLElement elementWithName:@"TYPE" stringValue:@"image/jpeg"];
NSXMLElement *binvalXML = [NSXMLElement elementWithName:@"BINVAL" stringValue:[[NSString alloc] initWithData:imageData1 encoding:NSUTF8StringEncoding]];
[photoXML addChild:typeXML];
[photoXML addChild:binvalXML];
[vCardXML addChild:photoXML];
XMPPvCardTemp *myvCardTemp = [[[self appDelegate] xmppvCardTempModule] myvCardTemp];
if (myvCardTemp) {
[myvCardTemp setPhoto:imageData1];
[[[self appDelegate] xmppvCardTempModule] updateMyvCardTemp:myvCardTemp];
}
Please help me to implement the upload image concept to XMPP or correct me If I am doing something wrong in the code.
Thanks in advance.
Hello I succeeded to have my avatar uploaded using your code but with some changes:
This class converts to base64 as Keith suggested
futhermore, I modified XMPPvCardTemp.h to expose this method:
Anyway, the XEP-0054 extension header
is requested.
Maybe this can be helpful for someone.
Hi I used a different way but it works very good. I made an interface where the user can select an image from gallery, I used ImagePicker to do this, then change the name of the image from the user JID, then u must upload the image to server... I used FTP but u can use a different way like HTTPS or something like that... At this point you have the image in server with the user's JID... now add SDWebImage to your project
hope this help you
Try converting your image to a
Base64
string value forBINVAL
instead of usingUTF-8
encoding.See: Convert Image to Base64 string in iOS
XML Syntax
According to the documentation, it is a requirement to use
Base64
:The <PHOTO/> element SHOULD contain a <BINVAL/> child whose XML character data is Base64-encoded data for the avatar image.
Image Restriction
The image data MUST conform to the base64Binary datatype [7] and thus be encoded in accordance with Section 6.8 of RFC 2045 [8], which recommends that base64 data should have lines limited to at most 76 characters in length. However, any whitespace characters (e.g., '\r' and '\n') MUST be ignored.
See also: XEP-0153: vCard-Based Avatars XML Syntax