iPhone/iPad Base64 Image Encoded - Convert to UIImage
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 fin开发者_开发问答e.
Ian
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:...]
.
I havent't tried but here there seems to be a working sample code ;)
Hope it helps
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);
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 );
精彩评论