【发布时间】:2016-04-11 12:14:49
【问题描述】:
各位程序员们好
我正在使用 Xamarin (iOS) 的应用程序中实现下载功能,以获取正在下载的文件的进度。
我使用的代码是来自网络的各种示例,显示相同的原理,最大的编码风格差异。它们都在 System.IO.File 上使用相同的 ReadAsync 和 WriteAsync 函数,并使用字节数组作为缓冲区。
但无论我转向哪个示例,它都会导致图片上出现不同的伪影,如下所示:
(原创/下载)
我似乎找不到发生这种情况的任何原因。 我尝试了不同的方法,看看是什么触发了这种情况。我发现将缓冲区更改为更大的大小(比如 10240)会导致更多的伪影,而更小的缓冲区(比如 1024)会导致更少的伪影。
在调试不同的东西时,我找到了一种“避免”这种情况发生的“方法”,但这包括在 WriteAsync 刚刚完成后添加一个任务延迟,持续 20 毫秒(在代码中注释掉) - 不是解决方案我想一起去,但告诉问题可能在于两个流的使用(在女巫我的知识是“仅限基本用法”)。 我也尝试使用非异步方法,但结果相同。
希望有人能告诉我我在这里做错了什么,以及为什么。
提前致谢
public async Task<bool> MakeDownloadRequestAsync(string url, string destination, Action<string, long, long, float> progress = null) {
if (_downloadHttpClient == null) {
_downloadHttpClient = new HttpClient (new NativeMessageHandler());
}
bool finished = false;
try {
var result = await _downloadHttpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
long receivedBytes = 0;
long totalBytes = result.Content.Headers.ContentLength.HasValue ? result.Content.Headers.ContentLength.Value : 0;
System.Diagnostics.Debug.WriteLine ("Started download file: {0}", url);
using (var stream = await _downloadHttpClient.GetStreamAsync (url))
{
byte[] buffer = new byte[4096];
var filename = Path.GetFileName (url);
var writeStream = _fileStore.OpenWrite (Path.Combine(destination, filename));
while(true)
{
int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
finished = true;
System.Diagnostics.Debug.WriteLine ("Finished downloading file: {0}", url);
break;
}
await writeStream.WriteAsync(buffer, 0, buffer.Length);
//Task.Delay(20);
receivedBytes += bytesRead;
if (progress != null)
{
progress.Invoke(url, receivedBytes, totalBytes, (float)receivedBytes/(float)totalBytes);
}
}
writeStream.Dispose();
stream.Dispose();
}
} catch (Exception e) {
System.Diagnostics.Debug.WriteLine (e);
}
return finished;
}
【问题讨论】:
-
能否提供手术前后的准确图片文件?
标签: c# xamarin xamarin.ios httpclient filestream