rahul raj
rahul raj

Reputation: 597

How to send uiimage to another device through json data or string?

I am new to iPhone development.Currently I am working on an application in which I need to send an image as well as text. So I thought of using json technology by sending the string as json data. Now I need to append this image to this string. Can anyone suggest a method to do this?

This application should also be able to bump with an android phone. Is there any method to do this?
I heard of converting the image to base64 and send as string. Is that the right method to do it?

Upvotes: 2

Views: 729

Answers (1)

silwar
silwar

Reputation: 6518

converting image to base64 is the right method for this. please have look at following code snippet

public String convertToBase64(Bitmap bm){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] image = baos.toByteArray();

    String encodedImage = Base64.encodeToString(image, Base64.DEFAULT);
    return encodedImage;
}

You can use this string to send through JSON data

Edited Part For iPhone Try this code

-(NSString *)getStringFromImage:(UIImage *)image{

 if(image){

  NSData *dataObj = UIImagePNGRepresentation(image);

   return [dataObj base64Encoding];

   } else {

  return @"";

 }
}

Hope this will solve your problem

Upvotes: 2

Related Questions