0

I have a base64 encoded image recieved via a web service.

How do I convert that string to a UIImage?

Obj-c or C# answers are fine.

Ian

Ian Vink
  • 66,960
  • 104
  • 341
  • 555

4 Answers4

2

First you need to convert the base64-encoded data into an NSData. This previous question seems to be a good resource on how to do that.

Then you just pass that NSData object to [UIImage imageWithData:...].

Community
  • 1
  • 1
Anomie
  • 92,546
  • 13
  • 126
  • 145
2

I havent't tried but here there seems to be a working sample code ;)

Hope it helps

nacho4d
  • 43,720
  • 45
  • 157
  • 240
2

In iPhone Monotouch C# this is how it is done:

byte[] encodedDataAsBytes = System.Convert.FromBase64String (Base64String);
string decoded = System.Text.Encoding.Unicode.GetString (encodedDataAsBytes);
NSData data = NSData.FromString (decoded, NSStringEncoding.ASCIIStringEncoding);
return UIImage.LoadFromData (data);
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
2

I was not able to get BahaiResearch's MonoTouch code to work -- an exception was thrown in NSData -- but was successful with the following:

byte[] encodedDataAsBytes = Convert.FromBase64String ( base64String );
NSData data = NSData.FromArray ( encodedDataAsBytes );                            
return UIImage.LoadFromData ( data );
BillF
  • 444
  • 1
  • 9
  • 15