3

I am using XMPPFramework for creating my chat app. I want to update the Avatar of the current user. I have used ImagePicker for this. Here's how my updateAvatar method looks like -

- (void)updateAvatar:(UIImage *)avatar
{
    NSData *imageData = UIImageJPEGRepresentation(avatar, 0.5f);
    imageData = [imageData base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_PRIORITY_DEFAULT);
    dispatch_async(queue, ^{
        XMPPvCardTempModule *vCardTempModule = self.xmppvCardTempModule;
        XMPPvCardTemp *myVcardTemp = [vCardTempModule myvCardTemp];
        if (!myVcardTemp)
        {
            NSXMLElement *vCardXML = [NSXMLElement elementWithName:@"vCard" xmlns:@"vcard-temp"];
            myVcardTemp = [XMPPvCardTemp vCardTempFromElement:vCardXML];
        }
        //[myVcardTemp setName:[NSString stringWithFormat:@"%@",name.text]];
        [myVcardTemp setPhoto:imageData];

        [vCardTempModule updateMyvCardTemp:myVcardTemp];

    });
}

But when I get the vCard back, the image turns out to be an invalid image. I have checked this question already. It's creating vCard element manually while I used setPhoto method on vCard. I tried with UIImagePNGRepresentation too, but it doesn't work either.

Any idea what could be wrong here?

Community
  • 1
  • 1
0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184

2 Answers2

3

are your overwriting your imageData object with this line??

imageData = [imageData base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];

use another NSData to wrap the base64encoded data, otherwise the result is unpredictable.

Allen
  • 6,505
  • 16
  • 19
2

Looks like I just needed to do this -

NSData *imageData1 = UIImageJPEGRepresentation(avatar,0.0);


XMPPvCardTemp *myvCardTemp = [self.xmppvCardTempModule myvCardTemp];

if (myvCardTemp) {
    [myvCardTemp setPhoto:imageData1];
    [self.xmppvCardTempModule updateMyvCardTemp:myvCardTemp];
}

Converting to Base64 was not required at all.

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184