I’m trying to convert UIImage to Base64 string, and send that to a web-service SOAP then a database.
So, I pick a picture with my app and I try to convert it with this method:
public static string GetBase64FromImage (UIImage picture_)
{
if (picture_ == null)
return string.Empty;
NSData data = picture_.AsJPEG();
Console.WriteLine("JPEG length: " + data.Length);
byte[] byteArray = new byte[data.Length];
System.Runtime.InteropServices.Marshal.Copy(data.Bytes, byteArray, 0, Convert.ToInt32(data.Length));
string base64 = Convert.ToBase64String(byteArray);
Console.WriteLine("Base64 length: "+base64.Length);
return base64;
}
My point here is the total length of the picture.
If I save the picture in file system, and import it, the picture size turns around 1 Mo.
But with my method, JPEG is the double of that (almost 2Mo). So my Base64 turns around 3 Mo which is huge.
Why NSData in JPEG is double size than the “normal” one ?
JPEG is a compressed data format. Depending on how you save the image (to a file, stream…) you can get different results.
AsJPEG()has an overload which accept afloat compressionQuality. If you do not supply a value the1.0fwill be used (as default). Since this calls intoUIImageJPEGRepresentationyou’ll get the least compression (best image but the largest size) by default. OTOH if you provide0.0you’ll get the maximum compression (smallest size).