XMPPFramework - Upload Profile or Avatar Image

ios, objective-c, xmpp, xmppframework

Solution

Try converting your image to a `Base64` string value for `BINVAL` instead of using `UTF-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

Problem

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.

Original source

Related problems