title: Xamarin IOS Encoding/Decoding Images from and to Base64
body: <p>I am working on an application for IOS in Xamarin. I have a menu in which I request something called "Doublechecks". These doublechecks have a field with the name "Medication". In a previous working copy of the app, I simply used a string to fill this field, but now we had the idea to, instead of filling this medication field with a string, to fill it with an image. One of the tips I got was to convert a taken or chosen image to base64.</p>
  
  <p>In the menu where I make a new doublecheck I have a button that pops up an actionsheet, where you can choose weather you want to pick an image from your PhotoLibrary, or take a new picture with your camera. When you've taken or chosen a picture, I use the following method to encode it to Base64:</p>
  
  <pre><code>var imageToSend = originalImage.AsJPEG (0.23f).GetBase64EncodedString (NSDataBase64EncodingOptions.None);
  </code></pre>
  
  <p>Where the originalImage is the image I took/chose. Now, when requesting all doublechecks, I use the following to decode it:</p>
  
  <pre><code>byte[] encodedDataAsBytes = System.Convert.FromBase64String (imageToDisplay);
  string decoded = System.Text.Encoding.ASCII.GetString (encodedDataAsBytes);
  NSData data = NSData.FromString (decoded, NSStringEncoding.ASCIIStringEncoding);
  return UIImage.LoadFromData (data);
  </code></pre>
  
  <p>The method works up until the return. Somehow, the UIImage is not being constructed, and has its value null, even though the 'data' contains the decoded string.</p>
  
  <p>I have searched several threads and fora, but without much avail. Any help would be much appreciated.</p>