我正在尝试使用base64编码将存储在应用程序文档文件夹中的图像内容读入字符串。我将图像位置作为url;因此,例如,我可以为该图像提供以下url:
file://localhost/var/mobile/Applications/40A88352-7F78-4085-856B-9621541774ED/Documents/tmp/photo_017.jpg这就是我试过的:
byte[] imgData = new WebClient().DownloadData(url);
string base64Encoded = System.Convert.ToBase64String(imgData);据我所知,这段代码应该是正确的。但是,这会导致我的单调应用程序在启动时崩溃,在调试器中,我看到引发了以下异常:
Mono.Debugger.Soft.VMDisconnectedException: Exception of type 'Mono.Debugger.Soft.VMDisconnectedException' was thrown.
at Mono.Debugger.Soft.Connection.SendReceive (CommandSet command_set, Int32 command, Mono.Debugger.Soft.PacketWriter packet) [0x00000] in <filename unknown>:0
at Mono.Debugger.Soft.Connection.VM_GetVersion () [0x00000] in <filename unknown>:0
at Mono.Debugger.Soft.Connection.Connect () [0x00000] in <filename unknown>:0
at Mono.Debugger.Soft.VirtualMachine.connect () [0x00000] in <filename unknown>:0
at Mono.Debugger.Soft.VirtualMachineManager.ListenInternal (System.Net.Sockets.Socket dbg_sock, System.Net.Sockets.Socket con_sock) [0x00000] in <filename unknown>:0 如果我注释掉了上面给出的两行代码,那么应用程序就会正确启动,所以在我看来,新的WebClient()代码行导致了这个异常。
因此,基本上,我需要知道,对于这个问题,我在WebClient中是否有解决办法,或者是否有另一种方法可以将图像的内容读入字符串,这样我就不需要使用WebClient了。
发布于 2011-06-04 12:35:12
如果您在启动时获得VMDisconnectedException,这可能意味着FinishedLaunching方法没有及时返回,iOS杀死了您的应用程序。
如果需要在启动时加载该文件,请将代码包装在允许FinishedLaunching及时返回的异步方法或线程中:
byte[] imgData;
string base64Encoded;
ThreadPool.QueueUserWorkItem(delegate {
imgData = new WebClient().DownloadData(url);
base64Encoded = System.Convert.ToBase64String(imgData);
});我尝试过您的代码,它可以工作,但是我建议尽可能多地对本地对象使用包装器。
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);
});https://stackoverflow.com/questions/6233379
复制相似问题