How to read the contents of a local image into a base64 string in MonoTouch

血红的双手。 提交于 2019-12-04 19:03:14

If you are getting VMDisconnectedException at startup, it probably means that the FinishedLaunching method does not return in time and iOS kills your app.

If you need to load that file at startup, wrap your code in an async method or thread that will allow FinishedLaunching to return in time:

byte[] imgData;
string base64Encoded;
ThreadPool.QueueUserWorkItem(delegate {
    imgData = new WebClient().DownloadData(url);
    base64Encoded = System.Convert.ToBase64String(imgData);
});

I tried your code and it works, however I would suggest using wrappers to native objects as much as possible.

byte[] imgData;
string base64Encoded;
ThreadPool.QueueUserWorkItem(delegate {
   NSUrl imageUrl = NSUrl.FromFilename("path/file");
   NSData data = NSData.FromUrl(imageUrl);
   imgData = data.ToArray();
   base64Encoding = Convert.ToBase64String(bufferData);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!